1. Use as initialized pointer
int main(void){
//使用未初始化的指针
int *p;
printf("*p value: 0X%p\n",*p);
return 0;
}
2. Assign the value to the pointer as an address
int main(void){
int *p;
int val = 10;
//将值赋给指针
p = val;
return 0;
}
3. Forget to dereference and directly access memory
int main(void){
int arr[10];
int *p1,*p2;
p1 = &arr[0];
p2 = &arr[1];
//判断数组两个元素的大小
if(p1>p2){
//内容永远不会被执行,因为比较的p1和p2的地址大小,p1永远小于p2
}
return 0;
}
4. Use again to ignore reassignment
int main(void){
char arr[10];
char *p;
p = arr;
do{
gets(arr); //控制台获取一组字符
while(*p){
printf("arr's value: %c\n",*p++);
}
}while(strcmp(arr,"done")!=0);
return 0;
}
Cause: p has pointers pointing to the array elements in the seventh end of the string "0 \", the next time print,
if the input is less than the length of the original content, or the pointer p pointing endings, and thus can not print
if The length of the input content is greater than the original, and the printed content is also displayed incorrectly
Solution: Reinitialize the pointer each time the loop
int main(void){
char arr[10];
char *p;
// p = arr;
do{
gets(arr); //控制台获取一组字符
p = arr; //每次循环进行初始化操作
while(*p){
printf("arr's value: %c\n",*p++);
}
}while(strcmp(arr,"done")!=0);
return 0;
}