#include "stdafx.h"#include"stdio.h"int main(int argc, char* argv[]){int *temp;int b=99;temp=&b;printf("temp is:%d\n",temp);printf("b is:%d\n",b);printf("b's addr is:%d\n\n",&b);*temp++=1;printf("temp is:%d\n",temp);printf("b is:%d\n\n",b);*temp++=1;printf("temp is:%d\n",temp);printf("b is:%d\n",b);return 0;}
Program running result:
Apparently, an exception occurs after the second execution of ++ in temp.
Modify the program as follows:
#include "stdafx.h"#include"stdio.h"int main(int argc, char* argv[]){int *temp;int b=99;temp=&b;printf("temp is:%d\n",temp);printf("b is:%d\n",b);printf("b's addr is:%d\n\n",&b);*temp++=1;printf("temp is:%d\n",temp);printf("b is:%d\n\n",b);temp++;printf("temp is:%d\n",temp);printf("b is:%d\n",b);return 0;}
Running result:
After modification, the program runs as expected.
Summary:
1. * temp ++ = 1 is interpreted as * temp = 1; temp ++;
2. A pointer points to an integer variable and performs the pointer ++ operation on the pointer. The value of the pointer has changed 4, not 1;
3. After the pointer value is changed, it points to a new address. If the new address is never declared as the address of any variable in the entire program, you cannot use the * operator to modify the content of this address.