Pointers and Pointer Operators in C (1)
Pointers and Pointer Operators in C (1)
Operators in C
by Prakhar mishra
Increment Operators: Incrementing Pointer
Values
Pre-increment (++p) Post-increment (p++)
Increments the pointer value before using it. Increments the pointer value after using it.
Decrements the pointer value before using it. Decrements the pointer value after using it.
Copies the address stored in one pointer to another pointer of the same type.
Before:
x: 10 at address 0x1000
int x = 10; y: 20 at address 0x1004
z: 30 at address 0x1008
int y = 20;
int z = 30; After:
int *p = &x; // p points to x (address of x) x: 10 at address 0x1000
y: 20 at address 0x1004
int *q = &y; // q points to y (address of y) z: 30 at address 0x1008
printf("Address of x: %p\n", &x); p points to 0x1008
printf("Address of y: %p\n", &y); q points to 0x1008
printf("Address of z: %p\n", &z);
printf("Value pointed to by p: %d\n", *p);
p++; // p now points to the next integer after x
printf("Value pointed to by p: %d\n", *p);
p += 2; // p now points to the integer 2 elements after x
printf("Value pointed to by p: %d\n", *p);
if (p == q) {
printf("p and q point to the same location\n");
}
q = p; // q now also points to the same location as p
printf("Value pointed to by q: %d\n", *q);
Thank You