C language pointer and array of details and differences

  • 2020-05-17 06:01:00
  • OfStack

C Pointers and arrays in detail and comparison

The general understanding of array Pointers and pointer arrays

Array pointer:

eg:int( *arr)[10];

A pointer to an array is a pointer to a variable.

Pointer array:

eg:int*arr[10];

A pointer array is simply an array of Pointers;

Arrays are not Pointers && Pointers are not arrays

(1) define an external variable:


eg:int value=10; int *p=&value;

Example: when this variable is needed in a function: externa int*p; Instead of extern int p[];

Analysis: when used :extern int p[]; In this form, it is actually: since this variable is defined as a pointer, p points to the address of value. When p is introduced as an array, it actually stores the address of value instead of pointing to value, resulting in an error in the call.

(2) define an external variable:


eg:int arr[10]={0};

Example: when it is necessary to introduce this external variable into a function: extern int arr[10]; Not extern int *arr;

Analysis: when using extern int *arr; This way introduced is, in fact, this is wrong, when the variables are defined as a plastic arrays, but at the time of statement is to use a pointer to the statement, statement can be accessed through the array to the whole array, but through the pointer is not pointing to the array pointer actually statement, but at arr [10] the first element in the array arr [0] address pointer, lead to access problems.

-- the relationship between a 2-dimensional array and a pointer

Define a 2-dimensional array arr[3][3], since the 2-dimensional array is also stored in memory in order, rather than as a matrix as we think. So, arr[3] is really equivalent to an array pointer; Pointing to the arr [0] [0], arr [1] [0], arr [2] [0].

Other differences between the strings array and Pointers

(1) the pointer stores the address of the data;

Arrays are used to hold data.

(2) the pointer accesses the data through the data address to dererate and access indirectly;

Array access to data is straightforward.

(3) Pointers are usually used for dynamic data structures;

Arrays are used to hold a fixed number of data of the same type.

(4) pointer usually points to anonymous data;

Arrays usually hold known ones.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: