Explain pointer arithmetic with example. Also explain advantages of malloc and calloc.
Pointer arithmetic is a fundamental concept in programming that involves performing arithmetic operations on pointers, which are variables that store memory addresses. It's commonly used in languages like C and C++ for various purposes, including memory management and array manipulation.
Here's an explanation of pointer arithmetic with an example:
Pointer Arithmetic:
Pointer arithmetic allows you to manipulate the memory addresses stored in pointers to access and modify data. It involves two main operations: addition and subtraction.
1. Addition: You can add an integer value to a pointer to move it to a new memory location. The size of the addition is determined by the data type the pointer is pointing to. For example, if you have an integer pointer `intPtr` pointing to an array of integers, you can move it to the next integer element like this:
```c
________ _________ ________ __________ ____ ___.
___ ____ ________ ____ ______ _________ ________ ________ _________ __________ _________.
__________ _______ ___ ______ _______ ____ ___ ____ ____ ________ __________.
__________ _____ ______ _________ _________ ________ ___ ______ _____ __________ _____ _____.
_________ ___ _________ ______ ___ _________.
______ _________ _________ _______ ___ ______ ________ ___ ______ _____ ________.
__________ ___ __________ ______ ____ _________ ______ ________ _______ ___ _______ ______.
____ __________ ______ ______ __________.
_______ ___ ____ ___ ______ _______.
______ _______ _______ ______ __________ ____ ______.
________ __________ ____ _________ ______ ____ ____ _________.
____ _______ _____ ________ ________ ___ ___ __________ _______ ___ ___ ______.
________ _______ _____ _____ ________ ____ ____ ____ _______.
________ ________ __________ ____ ________ ______ _______ ________ _____ __________ _______ _____.
_______ _______ ____ ___ _________ _________ __________ ________ _____.
____ __________ _________ ____ __________ ________ __________ ____ _________ ___ ___ ________.
________ __________ ____ _____ _______.
___ _____ __________ _________ ___ _____ _____ ___.
________ __________ _________ ____ ________ _____ ____.
_____ ____ _________ ___ ________ ______ _______.
_________ _________ ______ ________ __________ _____ __________ __________ ______.
_________ ___ _________ ________ ____ ____ _______.
______ ___ ________ __________ ________ ________ ___ ___ ________.
_________ _______ _______ _________ _______ ____ ________ ____ _________.
_____ ____ ______ _____ ______ _____.
______ ____ __________ ____ ______ ____ ________ _________.
___ _______ __________ __________ _____ __________ _____ ________ _______ ______ _______.
______ ___ ________ ____ _____ _________ ____ _____ _____ ____.
____ ____ ________ ___ _________ ______ _________ __________ _______ ______.
__________ _______ _____ _______ ______ ___.
____ __________ __________ _________ _____ _________.
____ _______ _____ _______ _____ _______ ___ ____ _____ _________.
__________ __________ _________ ______ ________ ______.
____ __________ ______ ____ ____ _____ ____ ________.
____ ________ ____ ________ __________.
__________ _________ _________ _______ __________ _____ _____ __________ ________ ____ _____ ___.
_____ ______ _____ _______ _______ __________ ________ __________ ____ ______.
________ _________ ___ _______ ____ ____ ________ ____ _____ __________.
___ _________ __________ ______ ________ _______ ____ _____ ________.
____ _____ ______ ___ __________ _______ __________ ________.
____ _____ _______ __________ ___.
______ ______ __________ _________ _______ _______ ________ ______ ______ ________ _______.
_____ _______ ______ _______ __________ _______ ________ ___ ___.
_____ ________ _____ ___ _______ _____ ____.
______ __________ _________ ________ _____ _________.
______ ____ ________ ____ ______ ____ ____.
___ ____ __________ _____ ________ __________ ____.
___ ___ ________ _____ _________ ___ _______ ______ _______.
________ _______ ____ _________ _______ _____.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required