#include <stdio.h>
int main()
{
int i = 5;
int* u = &i;
printf("%d\n", *(u + 0));
for(i = 0; i < 10; i++)
printf("%d\n", *u);
}
Output is:
输出是:
5
0
1
2
3
4
5
6
7
8
9
But I think it should print 5
11 times.
但我认为应该打印5次11次。
1 个解决方案
#1
2
As u contains the address of the variable i
any changes to i
will be reflected in the value of *u
. So going through the code :
由于u包含变量i的地址,对i的任何更改都将反映在* u的值中。所以通过代码:
#include <stdio.h>
int main()
{
int i = 5;
int* u = &i; //u contains the address of i so change in i changes *u
printf("%d\n", *(u + 0)); //prints the value of i as *u is the value i that is 5
for(i = 0; i < 10; i++) //the value of i changes so does *u.Therefore *u is incremented from 0 to 9 1 at a time.
printf("%d\n", *u); //prints the value of *u whch is effectively i
}
#1
2
As u contains the address of the variable i
any changes to i
will be reflected in the value of *u
. So going through the code :
由于u包含变量i的地址,对i的任何更改都将反映在* u的值中。所以通过代码:
#include <stdio.h>
int main()
{
int i = 5;
int* u = &i; //u contains the address of i so change in i changes *u
printf("%d\n", *(u + 0)); //prints the value of i as *u is the value i that is 5
for(i = 0; i < 10; i++) //the value of i changes so does *u.Therefore *u is incremented from 0 to 9 1 at a time.
printf("%d\n", *u); //prints the value of *u whch is effectively i
}