Increment and Decrement
When we increment an integer pointer by1, it increments by sizeof(int).
Example:
int i=20;
int *ptr=&i;
i ptr ptr++ ptr
20 1000 1002
1000 2058 or ++ptr 2058
If a pointer is decremented by 1, it is decremented by the size of the data item it points to.
When a pointer is incremented by 1 the new value will be (current address in pointer) + sizeof(data_type)
Pointer Arithmetic Continued …
Addition and Subtraction
‘C’ allows integers to be added to or subtracted from pointers. When an integer value i is added to a pointer, the new address in the pointer will be
(current address in pointer) + i* sizeof(data type)
Example:
int *ptr1, n;
ptr1=&n;
ptr1=ptr1+3;
This code will increment the contents of ptr1 by 3*sizeof(int). If the address in the pointer is 1000, the new address will be 1006. The same concept applies for decrementing a pointer.
Pointer Arithmetic Continued …
Differencing
Differencing is the subtraction of two pointers. The difference between two pointers indicates how far apart they are. It gives the number of elements between the two pointers. The result is:
(address in pointer1 – address in pointer2) / sizeof(data_type)
Example:
int n,*p,*q;
p=&n; //suppose address is 1000
q=p+2; //q now has 1004
printf(“%d”,q-p); // (1004-1000)/sizeof (int)
This code will yield a value of 2 even though numerically q and p differ.
Summary of pointer operations
Assignment
The value assigned should be an address
Indirection
Getting the value stored at an address.
Address of
The & operator used with a variable (or pointer) gives its address.
Increment
Incrementing a pointer by 1 adds sizeof(datatype) to the address in the pointer.
Decrement
Decreasing a pointer by 1 subtracts sizeof(datatype) from the address.
Addition and Subtraction
Adding or subtracting an integer i from a pointer increases/decreases the address by i*sizeof(datatype).
Differencing
Subtraction of two pointers gives the number of elements between two pointers.
Precedence of & and * operators & and * are unary operators. They have right to left associativity.
& and * are unary operators. They have right to left associativity.
Example 1 :
++*ptr
Example
int n=10,*ptr;
int n=10,*ptr;
ptr=&n;
ptr=&n;
printf(“%d”,++*ptr);
printf(“%d”,++*ptr);
Here it will fetch the value at ptr first and then increment it hence
Here it will fetch the value at ptr first and then increment it hence output will be 11.
output will be 11.
++ *ptr => ++(*ptr)
++ *ptr => ++(*ptr)
=>++(*1058)
=>++(*1058)
=>++(10)
=>++(10)
=>11
=>11
n ptr ++*ptr ptr n
n ptr ++*ptr ptr n
10 1058 1058 11
1058 2002 2002 1058
1058 2002 2002 1058
Example 2: *++ptr
int n=10,*ptr;
ptr=&n;
printf(“%d”,*++ptr);
Here ++ will be done first. This increments ptr by sizeof(int). Then the value pointed to by the address in ptr will be accessed. *++ ptr => *(++ptr)
=>*(++1058)
=>*(1060)
=>data_at_1060
n ptr *++ptr ptr Refers
10 1058 1060 data1058 2002 2002 to 1060
Example 3: *ptr++
Here also, ++ will be performed first. But since it is post-increment, it will first fetch the value pointed to and then increment ptr.
Relationship between Arrays and Pointers.
Also Read
- Static and Dynamic Memory Allocation
- Memory Leak and Dangling Pointer
- Memory Allocation for 2D Array
- Dynamic Memory Allocation
- Pointer Constant and Constant Pointer
- Pointer Declarations and their Meanings
- Functions and Pointers
- Initializing Pointer to Pointer
- Pointer to Pointer Multiple Indirection
- Relationship between Array and Pointer
- Pointer to Array
- Types of Pointers
- Illustrate Basic Pointer Use
- Using Pointer
- Intializing Pointer
- Declaring Pointer
- Application of Pointers
- Value Model VS Reference Model
- What is Pointer
Comments
Post a Comment
Please give us feedback through comments