This and that of "* p" in C language (* p ++ nightmare)

This and that of "* p"

When I learned C language pointers, I got confused around "* p", so I'll organize them.

First of all, the basic "* p"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*p...%d\n", *p);
}
//Output result:*p...10

This is easy. Enter the address of a [0] in p with "p = a". And I just take out the contents of a [0]. 「*p+1」

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*p+1...%d\n", *p+1);
}
//Output result:*p+1...11

This is not so difficult either. Just take out the contents of a [0] and add 1 to it. ("* (Reference) has a higher priority than" + "")

「*(p+1)」

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*(p+1)...%d\n", *(p+1));
}
//Output result:*(p+1)...20

Well, this time with parentheses. Parentheses are the highest priority operators. Therefore, "p + 1" is processed first. "P + 1" means that the address specified by p is advanced by one. That is, "& a [0] + 1", which is "& a [1]". Since "*" is attached to this, the contents are taken out and the output result is 20.

「*p+=1」

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*p+=1...%d\n", *p+=1);
}
//Output result:*p+=1...11

How about this? "Asterisk" (see) has a higher priority than "+ =". So, "* p" is processed first. "* P" is "a [0]", that is, "10". After that, "+ = 1" is set, so the output result is 11.

From here is the demon gate "* p ++"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*p++...%d\n", *p++);
}
//Output result:*p++...10

Many beginners will think it is "11", but that is a mistake. Combining increments and pointer references See and ** "pass the value" and then "increment the address" stored in the pointer **. ~~ (Dareka Oshete who couldn't understand the principle) ~~


[Addition] I received a comment saying, "You should understand the difference between putting an increment (++) before a variable (preface) and putting it after a variable (postscript)." Thank you very much.

Preface and postscript


...
int a, a_before, a_after;

a = 0;
printf("a...%d\n", a);

//Preface
a_before = ++a; 
printf("a_before(Preface,++a)...%d\n", a_before);
printf("Then a...%d\n", a);

//Postscript
a = 0;
a_after = a++; 
printf("a_after(After that, a++)...%d\n", a_after);
printf("Then a...%d\n", a);

...
//Output result: a...0
//         a_before(Preface,++a)...1
//Then a...1
//         a_after(After that, a++)...0
//Then a...1

As you can see from this sample code ・ Preface → Increment before passing the value ・ Postscript → Increment after passing the value There seems to be a difference. Naturally, I'm still ignorant.


Therefore, "10" is passed, and then the address stored in p is incremented.

Let's add a little to this.

python


...
printf("*p++...%d\n", *p++);
printf("After that*p...%d\n", *p);
...
//Output result:*p+...10
//After that*p...20

This makes it easier to understand. It's the most important in this article, so if I write it again, ** "After passing the value" It "increments the address in the pointer" **.

Well, of course, "(* p) ++"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("(*p)++...%d\n", (*p)++);
}
//Output result:(*p)++...10

Well you can see this. It is natural to be executed from inside the parentheses.

Amazing suction power "* (p ++)"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("*(p++)...%d\n", *(p++));
}
//Output result:*(p++)...10

~~ I don't know the reason, but this is the same as before. Overcoming the wall of parentheses, the value of "* p" is passed first without permission. I don't understand. Dine also has amazing suction power. Someone tell me ... ~~

Or rather, this grammatically doesn't work at all in parentheses. It will be very studying.

Let's change the location of the increment "*++ p"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a [0] = 10, a[1] = 20, a[2] = 30;
	printf("*++p...%d\n", *++p);
}
//Output result:*++p...20

I see. As expected, this seems to refer to the contents of the address of "incremented p" equal to "& a [0] + 1" ... "& a [1]". Therefore, "20".

Finally, let's increment at the front "++* p"

python


#include<stdio.h>
int main(void)
{
	int a[3], * p;
	p = a;
	a[0] = 10, a[1] = 20, a[2] = 30;
	printf("++*p...%d\n", ++*p);
}
//Output result:++*p...11

Hmmm, this seems to pass an increment of "* p" or "a [0]" (10). Therefore, "11".

Conclusion

Apparently, if you put an increment operator like "++" after ~~ "* p", it will be ignored once, pass the value, and then increment. ~~ It seems that putting the increment operator before and after determines whether or not to put the operation first. If you want to calculate first, bring the increment operator to the front. I learned a lot.

Recommended Posts