Pointer-Pointer Array, Array Pointer

Posted by subcool on Sun, 13 Feb 2022 18:21:06 +0100

Part1:

Pointer Array Usage Low-level Write Output Normal

Earlier, pointer arrays were mentioned as arrays, which hold pointers. Why does this happen? When we want to give many addresses to a pointer variable, we need a write, which is too cumbersome. The pointer array solves this problem. Take a look at the use of pointer arrays: and defining arrays of common types are common, except when assigning a value, the content becomes an address, the type plus *.

If we use it, we can use a loop to find the address of each pointer and then unreference 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 way of writing is more common than for beginners. We have many variables of type int. Why not write them as an array? The types of pointer arrays are also uniform and can only hold pointer variables of the same type. When we normally write arrays, we know the first address of the array to get the elements of the entire 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, and 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 Array Header Address
		int j = 0;
		for (j; j < 5; j++) {  //Find each array element based on the first address
			printf("%d ", *(parr[i] + j));  //output
		}
		putchar('\n');
	}

	return 0;
}

In order to gain a better understanding, our usual operations on arrays can also be performed on arrays of pointers

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

The results here are 24 and 3, respectively. I'm using x64, and the number of bytes the pointer takes is 8.

Part2:

Array pointer/array? Analogue First Element vs Array Binding

       

What is an array pointer? For example, an integer pointer is a pointer to an integer, and a character pointer is a pointer to a character. As the name implies, an array pointer is a pointer to an array.

       

First we need to know the address of the first element of the array and the address of the array, which was explained in detail in the beginner's pointer block, recall here.

        

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

Is this the pointer to the array?

	int* p = arr;

Or is it like this?

	int* p = &arr[0];

You'll find that they all point to the address of the first element of the array, not the address of the array (as mentioned in the beginners'pointer article), so what?

int* q = &arr;

Does this always point to an array address?
This seems reasonable, but it's not what we want. What we want is a pointer that can point to all the elements in the array, and there's still no way to point to each element's address with one pointer. So what about the next one?

int* p[] = { &arr[0],&arr[1],&arr[2]......&arr[8] ,&arr[9] };
//Point to an element with p[0],p[1], etc.

Does this point to all elements with one pointer? Hey, if you remember the pointer array you just saw, you can clearly see that it's a pointer array, that is, p[0],p[1] here, not a pointer, they're many pointers together, every eight bytes in memory in one (x64), imagine eighty bytes, every eight in a partition, Forms a pointer variable, which is an array of pointers. What the array pointer does is take these partitions away, put all the addresses in one space, and form a pointer with a space of eight crosses, which is the array pointer. Perhaps you had a question: isn't pointer space an address? How can I possibly point to all element addresses? Ha-ha, this is where array pointers differ from other types of pointers! How do I write the code?

The first thing to learn here is that the binding of a binding [] is higher than the * sign. If we write int*p[] directly, then the combination of P and [] is an array, what type? Int*. So what can we do to combine * with P first? Add one (). Int(*p)[]. * P first indicates that this is a pointer, then int [indicates that it is an integer array type pointer to an integer array. (This has to do with the designers, who want to express it this way for convenience as well as for writing, without adding more symbols. Imagine a bunch of rules and a bunch of symbols, using the arrangement of symbols to represent those rules. After constant optimization, symbols survive for ease of use and memory)

	int(*p)[10] = &arr;        //A pointer with 80 bytes of space

How do we unquote? We know it's a pointer, how does a normal pointer 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 pointers

}
//The results are:1

What about array pointers? This starts with a look at the combination.

As we have said before, what does this mean by combining the pointer outside of its definition? Array?

You must have known before that arrays are a special pointer. Its [means] that the address is computed and de-referenced as follows:

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

	printf("%d\n", arr[1]); //arr denotes the address of the first element, dereferencing after a + 1 operation
	printf("%d\n", p[1]);   //  p equals the address of the first element, dereferencing after a + 1 operation
 
	//Both operations result in:2
    
    return 0;
}

Let's look at this code again:

	int(*p)[10] = &arr;
	printf("%d\n",**p);  //You can see that p is a secondary pointer
                         //It needs to be dereferenced twice, but it's special
   //The results are:1

Let's look at this code again:

	printf("%p\n", p);
	printf("%p\n", *p);
    
    //Isn't it strange that programs run with exactly the same values?
    //It's a special place, and I don't know why it's designed like this

Obviously here p=&a, a weird secondary pointer, we give it a name: pointer to array, array pointer, type: int(*) [a construction type]. How should the correct code be described, 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]);  //Unquote, and we'll put a bracket here to make it a pointer to the first element
                               //It is then computed to get the address of a location and de-referenced.

	return 0;
}

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

//This opens up an 80-byte space and places the addresses of all elements in the arr from the beginning to the end.

//    An explanation of *p=p
//Pointers to arrays, designed to distinguish them from pointers to elements
//A simple array name represents a pointer to the first element, which requires a separate space to store
//At the same time, the value of the array name is immutable, if you want to represent the address of the entire array
//Just above the simple array name, raise the level to a second-level pointer, but don't give space alone
//Designing a rule that addresses an array name requires a variable to hold it and a space
//Instead of giving space alone, use the space occupied by the array name as its space, raise its level, and become a secondary pointer
//Mark this pointer with a type: type (*)[]
//When this secondary pointer is dereferenced at the same time, only its level is lowered and it becomes a primary pointer.


Topics: C C++ pointer