当前位置:网站首页>Pointer pointer array, array pointer

Pointer pointer array, array pointer

2022-07-06 00:19:00 It's Yi'an

Part1:

Pointer array usage   low-level write in Output normal

        Before , Pointer arrays have been mentioned , Is an array , An array of pointers . Why does this thing appear , When we want to give many addresses to pointer variables , Need to write one by one , It's too troublesome , Pointer array solves this problem . Let's take a look at the use of pointer arrays : And defining an array of common types , Only the content of assignment becomes the address , Type plus *.

Use the words , We can use a loop to find the address of each pointer , Dereference it .

#include<stdio.h>

int main(void) {
	int a = 1;
	int b = 2;
	int c = 3;
	int d = 4;
	int e = 5;
	int* parr[] = { &a,&b,&c,&d,&e };// Input , Type changed to int*, A pure array 
	
	int i = 0;    // Output 
	for (i = 0; i < 5; i++) {
		printf("%d ", *parr[i]);
	}

	return 0;
}

        This kind of writing is mostly for beginners , We have a lot of int Variable of type , Why not write it as an array ? The types of pointer arrays are also unified , Only pointer variables of the same type can be stored . We usually write arrays , Knowing the first address of the array can get the elements of the whole array , The same is true here , We only need a pointer variable to point to the first address of the array . Multiple elements point to multiple arrays of the same type , Put the pointers together into an array of pointers , A structure similar to a two-dimensional array appears

#include<stdio.h>

int main(void) {
	int a[] = { 1,2,3,4,5 };
	int b[] = { 2,3,4,5,6 };
	int c[] = { 3,4,5,6,7 };

	int* parr[] = { a,b,c };  // Input 

	int i = 0;
	for (i = 0; i < 3; i++) {   // Find the first address of the array 
		int j = 0;
		for (j; j < 5; j++) {  // Find each array element according to the first address 
			printf("%d ", *(parr[i] + j));  // Output 
		}
		putchar('\n');
	}

	return 0;
}

      In order to deepen our understanding of , The operations we usually perform on arrays can also be performed on pointer arrays

printf("%d\n", sizeof(parr));
printf("%d\n", sizeof(parr)/sizeof(parr[0]));

      The results obtained here are 24 and 3, I use it x64, The number of bytes occupied by the pointer is 8.

Part2:

Array pointer The pointer / Array ? analogy First element vs Array associativity

       

        What are array pointers ? Let's make an analogy , An integer pointer is a pointer to an integer , A character pointer is a pointer to a character , seeing the name of a thing one thinks of its function : An array pointer is a pointer to an array .

       

        We need to know the address of the first element of the array and the address of the array first , Before, there was a detailed description on the beginner's pointer , Let's recall .

        

	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	//arr--- First element address 
	//&arr[0]--- First element address 
	//&arr--- Array address 

      Pointer to array , Is it like this ?

	int* p = arr;

      It's still like this ?

	int* p = &arr[0];

      You will find that they all point to the address of the first element of the array , It doesn't point to the array address ( The difference is mentioned in the beginner's pointer ), What about that ?

int* q = &arr;

        This always points to the array address ?
      It seems reasonable , But it's not what we want , What we want is to use a pointer to point to all the elements in the array , It is still impossible to point to the address of each element with a pointer . Let's see what happens next ?

int* p[] = { &arr[0],&arr[1],&arr[2]......&arr[8] ,&arr[9] };
// use p[0],p[1] Wait to point to the element 

        Is this a pointer to all elements ? well , If you remember the pointer array you just saw , You can clearly find , This is an array of pointers , That is to say, here p[0],p[1], It's not a pointer , They are many pointers put together , From the perspective of memory, every eight bytes are placed in one block (x64), Imagine eighty bytes , Insert a partition every eight , Form pointer variables one by one , This is an array of pointers . What the array pointer has to do is take away these partitions , Put all the addresses in one space , Form a pointer with a space of 80 bytes , That is, array pointers . Maybe you have questions : Don't you put an address in the pointer space ? How can it point to all element addresses ? ha-ha , This is the difference between array pointers and other types of pointers ! How to write the code ?

        Here is a combination [ ] The combination of is higher than * The no. , If we write directly int*p[], such p and 【】 combination , Is an array , What kind of ?int*. Then we can make * and p First combine , How do you do it? ? Add one more ().int(*p)[ ].*p First, it means that this is a pointer , After use int【】 Represents a pointer to an integer array type , Points to an integer array .( This is related to the designer , They want to express it in this way for the convenience of writing , No more symbols , Imagine a pile of rules and symbols , Use the arrangement of symbols to represent those rules , After continuous optimization , The surviving symbols are for convenience of use and memory )

	int(*p)[10] = &arr;        // A space is 80 Byte pointer 

How do we dereference ? We know it is a pointer , How do general pointers dereference ?

#include<stdio.h>
int main (void)
{
    int arr[]={1,2,3,4,5,6,7,8,9,10};
    int* p=arr;
    printf("%d",*p); // Dereference of integer pointer 

}
// The result is :1

How to do array pointer ? First, we need to understand the combination .

I mentioned the problem of associativity before , This 【】 What does it mean to combine with the pointer outside the definition ? Array ?

You must have known before that array is a special pointer . Its 【】 It means to calculate and dereference the address , as follows :

#include<stdio.h>
int main(void) {
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;          // Define an integer pointer to the address of the first element         

	printf("%d\n", arr[1]); //arr Represents the address of the first element , Conduct +1 Dereference after operation 
	printf("%d\n", p[1]);   //  p Equal to the first element address , Conduct +1 Dereference after operation 
 
	// The results of both operations are :2
    
    return 0;
}

Let's take a look at this code :

	int(*p)[10] = &arr;
	printf("%d\n",**p);  // You can find p It's a secondary pointer 
                         // Need to dereference twice , But there is something special 
   // The running result is :1

Let's look at this code :

	printf("%p\n", p);
	printf("%p\n", *p);
    
    // The values obtained by running the program are exactly the same , Is it a little weird 
    // This is a special place , I don't know why it is designed like this 

Obviously here p=&a, A weird secondary pointer , We gave it a name : Pointer to array , Array pointer , type :int (*)【】, A construction type . How should the correct code describe , as follows :

#include<stdio.h>

int main(void) {
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

	int(*p)[10] = &arr;
	printf("%d", (*p)[0]);  // Quoting , Let's add a bracket here , Make it a pointer to the first element 
                               // Then we calculate it to get the address of a certain location , And then I'll explain the quotation .

	return 0;
}

	int(*p)[10] = &arr;

// A piece of land has been opened up here 80 Byte space , And put arr From the beginning of the first element to the end of the element, the address is put in it 

//    *p=p An explanation of 
// Pointer to array , The designer made this design in order to distinguish it from pointers to elements 
// A simple array name represents a pointer to the first element , It needs to open up a separate space for storage 
// At the same time, the value of the array name is immutable , To represent the address of the entire array 
// Just above the simple array name , Improved the level , Change to secondary pointer , But don't give space alone 
// Design a rule , When addressing an array name , You originally need a variable to store it , Need a space 
// Don't give space alone , Take the space occupied by the array name as its space , Raise its level , Change to secondary pointer 
// Mark this pointer , Mark with a type :type(*)[]
// When dereferencing this secondary pointer at the same time , Just lower its level , Become a first level pointer 


原网站

版权声明
本文为[It's Yi'an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140232467821.html