C language learning - the difference between array pointers and pointer arrays

1. Definition of array pointer and pointer array

1. Array pointer: define int (*p)[n];
because () has a high priority, first explain that p is an int type pointer, which points to a one-dimensional array of integer (int), this one-dimensional array The length of is n, it can also be said that there are n grids in total. Array pointers are also called pointers to one-dimensional arrays, also known as row pointers.
Array pointer can also be called "array pointer". Firstly, this variable is a pointer. Secondly, "array" modifies this pointer, which means that this pointer stores the first address of an array, or that this pointer points to the first address of an array. address.

2. Pointer array: define int *p[n];
the pointer array can be said to be an "array of pointers". Since *p has no brackets, firstly this variable is an array, and secondly, "pointer p" modifies this array, which means It is said that all elements of this array are of pointer type, and the number of bytes occupied by the pointer has nothing to do with its type, but only with the system. In a 32-bit system, any type of pointer occupies 4 bytes. Under a 64-bit system , any type of pointer occupies 8 bytes.


Second, the use of array pointers and pointer arrays

1. Array pointer
First define an array pointer. Since it is an array pointer, it means that the pointer p is used to point to a one-dimensional array, namely:

int (*p)[[4]];
//The int type is defined here, p is a pointer pointing to the first address of a one-dimensional array, and the size of this one-dimensional array is 4

That is to say, the pointer p of type int points to the array of int [[4]], and each element of the array is a variable of type int.

The essence of an array pointer is a pointer, and the function of a pointer is to point to the address of a variable, so an array pointer can generally be used like this (example):

void fun(int (*P)[4]); //parameter in the sub-function, array pointer 


a[3][4] = {0}; //The two-dimensional array fun(a) defined in the main function ; //The actual parameter of the main function calling the sub-function is the address of the first element of the two-dimensional array

void fun(int (*P)[4]); //子函数中的形参,数组指针 

a[3][4] = {0}; //主函数中定义的二维数组
fun(a); //主函数调用子函数的实参,是二维数组的首元素首地址


2. Array of pointers
For an array of pointers, that is, "array of pointers", it means that all elements of this array are of pointer type. Let's define a pointer p, for example:

int *p[[4]];
//here the length of the array is 4, all elements are pointers of type int

The code is as follows (example):

void Fun(char** str);    //子函数中的形参,指针数组

int main()
{
	int arr[3][4];
	char* str = "abc";
	char** q = &str;
}


Pointer array and array pointer parameter transfer problem
Passed parameters match the required formal parameters 

passed parameters Match required parameters
int *p[4] int **p (pointer array or secondary pointer)
int (*p)[4] int (*p)[4] (unchanged)
int [3][4] int (*p)[4] (array pointer)
int **p int **p (unchanged)


3. Examples of actual use

Example: Use pointer array to print out: "sun wu kong"

// 代码块  打印“sun wu kong”
char str1[] = { 's', 'u', 'n' , '\0' };
char str2[] = { 'w', 'u' ,'\0' };
char str3[] = { 'k', 'o', 'n', 'g' ,'\0' };
char* p[3] = { str1, str2, str3 };

Summarize

Tip: Here is a summary of the article:
1. Array pointer is simply understood as "array pointer". First, this variable is a pointer. Second, "array" modifies this pointer, which means that this pointer stores the first address of an array. In other words, this pointer points to the first address of an array.
2. A pointer array is simply understood as an "array of pointers". First, this variable is an array, and second, "pointer p" modifies this array, which means that all elements of this array are pointer types.
3. Questions about function parameter passing: Passed parameters: int *p[4] -> Formal parameters required for matching: int **p (pointer array or secondary pointer); passed parameters: int (*p)[ 4] -> Formal parameters required for matching: int (*p)[4] (unchanged).
 

 

Guess you like

Origin blog.csdn.net/qq_40999917/article/details/128336734