An in-depth analysis of the pointer "Five"

Today I mainly talk about pointer arrays, and knowledge about array pointers

 

Hope you all have fun today too!

 

content

array of pointers:

How about a little advanced?

Extension:

Are the numbers in the array part of the type? (10 in int[10])

 So what about forcibly forwarding the address?

Next notice:


array of pointers:

The first is an array. The elements of the array are pointers, and the number of bytes occupied by the array is determined by the size of the array itself. ,

Arrays can hold any type (integers, floats, structs, bit fields, pointers, arrays, etc.)

Array pointer:

The first is a pointer, which points to an array, which is 4 bytes under 32 bits, and the number of bytes of the pointed array is unknown.

A pointer can point to any legal type variable.

    int *p1[10] ;
	int(*p2)[10] ;

Analyze the above example:

First [ ] has higher precedence than * So, p1 first combined with [ ] is an array, and then the type is the type int * of the array.

p2 is first combined with * to become a pointer, pointing to an array, the type of the array is int [10].

How about a little advanced?

    int(*p3[7])[10];
	int(*(*p4)[8])[10];

This looks a bit complicated, but the essence has not changed, let's analyze:

First of all, p3 is first combined with the array, it is an array, then the content must be placed, the type is int (*) [10], this type is an array of pointers, so this is an array of array pointers , the number of array elements is 7, The array element type is int (*) [10].

p4 is first combined with the pointer, it is a pointer, the pointer must have a pointer, point to an array, the array stores an array pointer, so this is a pointer to an array pointer array , the pointer first points to an array pointer, and the content of the array pointer is an array.

Here you can wirelessly nest dolls, but in general, this step will not be used, as long as you can understand it.

Extension:

There is a strange place in C language, normally, a variable is type + variable name = value . Integer, floating point, and pointer are all satisfied, but putting it in an array is different ! 

See an example:

    int a = 10;
	double b = 11.1;
	int * c = NULL;
	double *d = NULL;
	int arr[10] = { 0 };
	double brr[10] = { 0 };

The general types are on the left, while the arrays are different, but they all satisfy the type by removing the variable name.

So if we want the type of the array to be the same as others, then we use typedef to rename the type.

 This makes sense, but the use of typedef is a kind of encapsulation, and it is not recommended to use it excessively .

Are the numbers in the array part of the type? (10 in int[10])

 

In this way, through debugging, the [ ] number in the array is part of the type, and it is put into b without warning after &a, which proves that the type of &a is the same as that of the pointer variable b, if the numbers are different, you can put it in b go in?

You can put it in, but there is a warning.

 

 This is related to another point of knowledge, about coercive conversion of addresses.

Coercion: only changes the way the data is viewed, not the data type.

For example, if the string "12345" is converted to the integer 12345, it cannot be achieved by forced conversion, then the data has changed. In for example int a = 97, you want to represent it as the character 'a', then you only need to look at the data of type char, without changing the array itself.

 

 So what about forcibly forwarding the address?

In fact, the value of the address does not change , only the number of bytes accessed is changed, or it is related to the access step size after the pointer is forced.

 

 

Next notice:

The next issue will explain the knowledge of two-dimensional arrays.

The next issue is more exciting! ~! ~!

 

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124238246