Operations with pointers:
User can perform below operation with pointers:
User can perform below operation with pointers:
- Adding an integer to pointer
- Subtracting an integer from pointer
- Subtracting one pointer from another
- Comparing two pointers
Adding an integer to pointer:
Consider ptr as a pointer to the array element arr[i], then ptr++ increments ptr to point to the array element arr[i + 1]. Similarly, if ptr points to the array element arr[i], and j is an integer, then "ptr = ptr + j" makes the pointer "ptr" to point to the array element arr[i + j].
#include <stdio.h>
int main() {
int *ptr, i = 0, arr[5] = {10, 20, 30, 40, 50};
ptr = &arr[i];
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
++ptr, i++;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
++ptr, i++;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
ptr = ptr + 2;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i + 2, arr[i + 2]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i + 2, (int)&arr[i + 2]);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
*ptr: 10 arr[0]: 10 ptr: 0xbfcd1724 &arr[0]: 0xbfcd1724
*ptr: 20 arr[1]: 20 ptr: 0xbfcd1728 &arr[1]: 0xbfcd1728
*ptr: 30 arr[2]: 30 ptr: 0xbfcd172c &arr[2]: 0xbfcd172c
*ptr: 50 arr[4]: 50 ptr: 0xbfcd1734 &arr[4]: 0xbfcd1734
*ptr: 10 arr[0]: 10 ptr: 0xbfcd1724 &arr[0]: 0xbfcd1724
*ptr: 20 arr[1]: 20 ptr: 0xbfcd1728 &arr[1]: 0xbfcd1728
*ptr: 30 arr[2]: 30 ptr: 0xbfcd172c &arr[2]: 0xbfcd172c
*ptr: 50 arr[4]: 50 ptr: 0xbfcd1734 &arr[4]: 0xbfcd1734
Subtracting an integer from pointer:
Consider ptr as a pointer to the array element arr[i], then ptr-- decrements ptr to point to the array element arr[i - 1]. Similarly, if ptr points to the array element arr[i], and j is an integer, then ptr = ptr - j makes the pointer ptr to point to the array element arr[i -j].
#include <stdio.h>
int main() {
int *ptr, i = 4, arr[5] = {10, 20, 30, 40, 50};
ptr = &arr[i];
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
--ptr, i--;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
--ptr, i--;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);
ptr = ptr - 2;
printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i - 2, arr[i - 2]);
printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i - 2, (int)&arr[i - 2]);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
*ptr: 50 arr[4]: 50 ptr: 0xbf9ad2d4 &arr[4]: 0xbf9ad2d4
*ptr: 40 arr[3]: 40 ptr: 0xbf9ad2d0 &arr[3]: 0xbf9ad2d0
*ptr: 30 arr[2]: 30 ptr: 0xbf9ad2cc &arr[2]: 0xbf9ad2cc
*ptr: 10 arr[0]: 10 ptr: 0xbf9ad2c4 &arr[0]: 0xbf9ad2c4
*ptr: 50 arr[4]: 50 ptr: 0xbf9ad2d4 &arr[4]: 0xbf9ad2d4
*ptr: 40 arr[3]: 40 ptr: 0xbf9ad2d0 &arr[3]: 0xbf9ad2d0
*ptr: 30 arr[2]: 30 ptr: 0xbf9ad2cc &arr[2]: 0xbf9ad2cc
*ptr: 10 arr[0]: 10 ptr: 0xbf9ad2c4 &arr[0]: 0xbf9ad2c4
Subtracting one pointer from another:
Subtracting two pointers of same type ptr2 - ptr1 gives us the number of elements between the two pointers ptr1 and ptr2. Below is an simple program that illustrates subtraction operation on pointers.
#include <stdio.h>
int main() {
int num, *ptr1, *ptr2;
int arr[5] = {10, 20, 30, 40, 50};
/* assigning address to pointers */
ptr1 = &arr[3];
ptr2 = &arr[0];
/* subtracting two pointers of same type */
num = ptr1 - ptr2;
printf("Number of elements between ptr1 and ptr2 is %d\n", num);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Number of elements between ptr1 and ptr2 is 3
Number of elements between ptr1 and ptr2 is 3
Comparing two pointers:
Comparison operations can also be performed on pointers. Consider two pointers ptr1 and ptr2 points to members of same array, then
- If ptr1 is equal to ptr2, then both ptr1 and ptr2 points to same element in the given array.
- If ptr1 is greater than ptr2, then ptr2 points to the earlier member in the given array.
- If ptr1 is lesser than ptr2, then ptr1 points to the earlier member in the given array.
#include <stdio.h>
int main() {
int *ptr1, *ptr2;
int arr[5] = {10, 20, 30, 40, 50};
/* assigning same address to pointer ptr1 and ptr2 */
ptr1 = ptr2 = &arr[0];
if (ptr1 == ptr2) {
printf("Condition 1:\n");
printf("ptr1 and ptr2 points to same element of the array\n");
printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
}
/* advancing pointer ptr1 */
ptr1 = ptr1 + 2;
if (ptr1 > ptr2) {
printf("\nCondition 2:\n");
printf("ptr2 points to earlier element in the array\n");
printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
}
/* advancing pointer ptr2 */
ptr2 = ptr2 + 4;
if (ptr2 > ptr1) {
printf("\nCondition 3:\n");
printf("ptr1 points to earlier element in the array\n");
printf("ptr1: 0x%x\t*ptr1: %d\n", (int)ptr1, *ptr1);
printf("ptr2: 0x%x\t*ptr2: %d\n", (int)ptr2, *ptr2);
}
return 0;
}
Output:
Condition 1:
ptr1 and ptr2 points to same element of the array
ptr1: 0xbf83c374 *ptr1: 10
ptr2: 0xbf83c374 *ptr2: 10
Condition 2:
ptr2 points to earlier element in the array
ptr1: 0xbf83c37c *ptr1: 30
ptr2: 0xbf83c374 *ptr2: 10
Condition 3:
ptr1 points to earlier element in the array
ptr1: 0xbf83c37c *ptr1: 30
ptr2: 0xbf83c384 *ptr2: 50
ptr1 and ptr2 points to same element of the array
ptr1: 0xbf83c374 *ptr1: 10
ptr2: 0xbf83c374 *ptr2: 10
Condition 2:
ptr2 points to earlier element in the array
ptr1: 0xbf83c37c *ptr1: 30
ptr2: 0xbf83c374 *ptr2: 10
Condition 3:
ptr1 points to earlier element in the array
ptr1: 0xbf83c37c *ptr1: 30
ptr2: 0xbf83c384 *ptr2: 50
Can we add two pointers in C?
No, addition of two pointers is not allowed.
#include <stdio.h>
int main() {
int *ptr1, *ptr2;
int arr[5] = {10, 20, 30, 40, 50};
/* adding two pointers */
ptr1 = ptr1 + ptr2;
return 0;
}
Output:
jp@jp-VirtualBox:~/$ gcc addPtr.c
addPtr.c: In function ‘main’:
addPtr.c:7: error: invalid operands to binary + (have ‘int *’ and ‘int *’)
addPtr.c: In function ‘main’:
addPtr.c:7: error: invalid operands to binary + (have ‘int *’ and ‘int *’)
No comments:
Post a Comment