Article directory
1, uninitialized pointer
A pointer is created, but not assigned a value.
#include <stdio.h>
int main() {
int *a;
*a = 3;
return 0;
}
Effect: The compiler will report an error.
2. Operate the array, the pointer exceeds the length of the array
pointer, but out of bounds.
The length is 3, but the fifth address is operated.
#include <stdio.h>
int main() {
int a[] = {
1, 2, 3};
int *b = a;
*(b + 4) = 6;
printf("%d\n", *(b + 4));
return 0;
}
Effect: report an error.
3. The pointer exists, but the data has left
#include <stdio.h>
int *abc() {
int a = 3;
return &a;
}
int main() {
int *a = abc();
*a = 4;
printf("%d\n", *a);
return 0;
}
Effect: report an error.
4. Null pointer
You can assign NULL to a pointer to represent a null pointer.
#include <stdio.h>
int main() {
int *a = NULL;
printf("%d\n", a == NULL);
int b = 3;
a = &b;
printf("%d\n", a == NULL);
return 0;
}
Effect:
It is NULL at the beginning, and after the assignment, it is no longer NULL.
Assigning null and judging null are more common practices.