Simply analyze the difference between pointer arrays and array Pointers in C language

  • 2020-05-05 11:38:30
  • OfStack

Let's start with a small example of an array of Pointers:


#include <stdio.h> 
#include <string.h> 
 
int lookup_keyword(const char*key, const char* table[], const int size) 
{ 
  int ret = -1; 
 
  int i = 0; 
 
  for(i=0; i<size; i++) 
  { 
    if (strcmp(key, table[i]) == 0) 
    { 
      ret = i; 
      break; 
    } 
  } 
  return ret; 
} 
 
#define DIM(array) (sizeof(array)/sizeof(*array)) 
 
int main() 
{ 
  const char* keyword[] = { 
      "do", 
      "for", 
      "if", 
      "register", 
      "switch", 
      "while", 
      "case", 
      "static", 
  }; 
 
  printf("%d\n", lookup_keyword("static", keyword, DIM(keyword))); 
 
  return 0; 
} 

Array pointer:


#include <stdio.h> 
 
int main() 
{ 
  int i; 
  int* pI = &i; // Common type  
 
  typedef int(AINT5)[5]; 
  AINT5* p1; 
  int array[5]; 
  p1 = &array; // Pointer to an array 1 
 
  int (*p2)[5] = &array; // Pointer to an array 2( This is not recommended ) 
 
  int (*p3)[4] = &array; // X  Pointer to an array 3( This is not recommended ) 
 
  return 0; 
} 

The two names of course mean different things. I was shocked when I first saw this, mainly because the Chinese language is so broad and profound, the whole abbreviation is so professional and confusing. It is easy to understand from the English explanation or the Chinese full name.

Pointer array: array of pointers, that is, the array used to store Pointers, that is, the array elements are Pointers

Array pointer: a pointer to an array, the pointer to the array

Also note the differences in their usage, as illustrated by the following examples.

int* a[4]         pointer array        

                     

The                             element means: *a[i]     *(a[i]) is the same because [] priority is higher than *

int (*a)[4]     array pointer        

                   

The                 element says :(*a)[i]  

Note: in practice, we often use

for pointer arrays

typedef int* pInt;
pInt a[4];

This is the same as the pointer array definition above, but with a type transformation.

The code is illustrated as follows:


#include <iostream>
 
using namespace std;
 
int main()
{
int c[4]={1,2,3,4};
int *a[4]; // Pointer to an array 
int (*b)[4]; // Pointer to an array 
b=&c;
// The array c Is assigned to an array a
for(int i=0;i<4;i++)
{
a[i]=&c[i];
}
// So let's look at the output 
cout<<*a[1]<<endl; // The output 2 on 
cout<<(*b)[2]<<endl; // The output 3 on 
return 0;
}

Note: defines the array pointer, the pointer to the array first address, must to the designated address of a pointer, is easy to make mistake, not to give b address directly with (* b) [i] = c [i] to the element in the array b assignment, don't know where to direct the array pointer, the debugging may be right, but the runtime must appear problem, should pay attention to this problem when using Pointers. But why don't a give him an address? The element of a is a pointer. But if you write *a[i]=c[i] in the for loop, this is also a problem. In a word, if you define a pointer, you have to know where it's pointing, or it's a tragedy.


Related articles: