Location>code7788 >text

A summary of C pointer types

Popularity:76 ℃/2024-10-24 23:01:16

Concept of wild pointer: pointer points to an unknowable location (random, incorrect, not explicitly limited)

The following describes several wild-pointer scenarios

① Pointer not initialized

#include <>
int main()
{
 int *p;// local variable pointer not initialized, defaults to random value

 *p = 20; //Contains a randomized value for the local variable pointer.
  return 0; }
}

② Pointer out-of-bounds access

 #include <>
 int main()
 {
   int arr[10] = {0}
   int *p = &arr[0];
   int i = 0;
 for(i=0; i<=11; i++)
 {
      //p is a wild pointer when the pointer points to an area beyond the range of the array arr
  *(p++) = i;
  }
   return 0;

③ The space pointed to by the pointer is released

 #include <>
 int* test()
 {
 int n = 100;
 return &n;
 }
 int main()
 {
 int*p = test();
 printf("%d\n", *p);
 return 0;
 }

The space requested by variable n is returned to the operating system when you exit the function.
There exists no space pointed to by the address in p