I’m a beginner in C and Let’s say I have a code like this:
#include <stdio.h>
void test(char *t)
{
t++;
*t = 'e';
}
void main()
{
char a[] = "anto";
printf("%c\n",a[1]);
test(a);
printf("%c\n",a[1]);
}
This is the sample code, where I am figuring out how pointers work. According to me the statement:
t++;
in the above code will increment the address of array a by 1 char in the calling function test. Fine, now as far I know the * is used to retrieve the object value that the pointer points to.
But weirdly when I change the t++ to
*t++;
I’m getting the same output as before. I’m literally confused with this, the above statement
*t++; should change the contents only know, according to the definition of * operator.
But again this changes the address of t. How come? Where I’m getting the concept wrong?
Thanks in advance
The expression
*t++is parsed as*(t++)— the++still applies to the pointer, not the contents. The value oft++is the value of the pointer itself before the increment, while the value of*t++is what the pointer points to before the increment.