Structure Pointer Data Pointer Array Array Pointer

1. Structure pointer

struct h
{
BTNode *TQ;
int level;
}a[100], b, *c[3],  *d;

 
a is a structure array, which already has 100 nodes. No need to allocate space anymore.
b is a structure variable, and it already has space, just like int b; already has space.
c is an array of pointers, and c already has space, with 3 elements, but these three elements are pointers that have not yet allocated space, so we need to use malloc to allocate memory for them respectively.
d is a structure pointer, like int *d; memory space must be allocated for him;
in fact, the structure can be understood as a data type similar to int, and its operations in defining variables and pointers are similar to those of int The basic data types are the same.

Structure pointer array_win9zz's blog-CSDN blog_Structure pointer array

2. Array of pointers

If all elements in an array hold pointers, then we call it an array of pointers. Its general form is:

            Data type *array name[constant expression][constant expression]...... ;

It is an array, and the elements of the array are all pointers. How many bytes the array occupies is determined by the size of the array itself, and each element is a pointer.

 For example: char *arr[]={"Sunday", "Monday"}, two pointers are stored, the first pointer points to the string "Sunday", the second pointer points to the string "Monday", and sizeof (arr)=8, because on the 32-bit platform, the size of the pointer type occupies 4 bytes. The most important use of an array of pointers is to perform operations on multiple strings, because character pointers are faster and more efficient than two-dimensional arrays.
Detailed Explanation of Pointer Array and Array Pointer (Comprehensive Knowledge Points)

3. Array pointer (for two-dimensional arrays)

Note: Because the use of array pointers for one-dimensional arrays is awkward, for one-dimensional arrays, it is more convenient to use pointer arrays. This only involves knowledge about two-dimensional arrays and array pointers! ! !

First introduce the definition of a two-dimensional array: a two-dimensional array is conceptually two-dimensional, with rows and columns, but all elements in memory are arranged consecutively

Detailed Explanation of Pointer Array and Array Pointer (Comprehensive Knowledge Points)

In order to better understand the difference between array pointers, ordinary pointers and secondary pointers, the following examples are given to illustrate.

For example:

{

 int a[4][5];

    int (*p)[5]=a;

}

Here a is the array name of a two-dimensional array, which is equivalent to a second-level pointer constant; //The two-dimensional array name has nothing to do with the second-level pointer (beginners are often confused).

p is a pointer variable , which points to a one-dimensional array containing 5 int elements, and the increment of p at this time takes the length of the one-dimensional array it points to as the unit;

p+i is the address of the one-dimensional array a[i], that is, p+i==&a[i]; the content operation (*) on both sides of the formula gets *(p+i)==a[i], Since a[i]==&a[i][0] in the two-dimensional array, *(p+i) represents the address of a[i][0], that is, *(p+i)==&a[i] [0];

*(p+2)+3 means a[2][3] address (the first line is 0 rows, the first column is 0 columns), *(*(p+2)+3) means a[2][3 ] value.

//(*p)[5] where 5 is replaced by other numbers, it cannot be compiled in the vc++6.0 environment

(*p)[5] The 5 in the above example does not mean anything, you can replace it with an integer other than 0, and the function of [5] is to help you remember the length of the one-dimensional array you are pointing to . (However, except for the length consistent with the defined two-dimensional array , no warning will be issued) Other numbers will warn but will not affect the result.

Guess you like

Origin blog.csdn.net/luyao3038/article/details/128313731