I was trying to understand pointers in C and was trying a code sample, which gave an absurd result.
Following is the code sample :
void main()
{
int *a;
int arr[5] = {1,3,4,5,2};
printf("%d\n", *(arr+1));
a = arr;
printf("Address: %p || Value: %d\n", ++a, *a);
}
okay so I expected it to output the address
and value
of second index of the array but unfortunately the output value
isn't not equal to 3 (the second index value of the array) as opposed to the address which comes out correct.
The problem here is that the prefix ++a
expression doesn't update the pointer variable a
and when later in the printf function a
is dereferenced
the updated value is not used and hence the output shows the first index value of the array.
Please help me on this.