# Pointer array and array pointer

```#include<stdio.h>
int main(){
int *p1[10];//p1 is first combined with [], indicating that it is an array, and the element type is int *, so it is an integer pointer array. Any element of the same type in the array can be placed
int(*p1)[10];//p1 is first combined with * to indicate that it is a pointer variable. The target pointed to is an array. There are 10 elements, each of which is int
return 0;
}

```

# Discussing the difference between array name and & array name

```#include<stdio.h>
int main(){
int a[10] = { 0 };
return 0;
}
```

Q: is the number of elements of the array part of the array type?
Explanation:

This proves that a is the address of the first element of the array, not the address of the whole array. int(*p)[10] is an array pointer. What should be stored is the address of the whole array!!

That's no problem.
After understanding a and & A, explain: is the number of elements of the array part of the array type?

The left and right sides are different. Conclusion: the number of array elements is a part of the array type!!!

# Forced type conversion of pointer

Let's talk about the meaning of forced type conversion first. In fact, this forced type conversion only changes how you view the binary sequence stored in memory. Note that it is "view", not change

What about the strong rotation of the pointer?

&After the forced conversion of a, it is still int *, actually & A is stored in the pointer variable p. it is OK not to force the conversion. The forced conversion is to tell the compiler and the programmer

## Strong turn to question 1

```#include <stdio.h>
#include <windows.h>
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p = (struct Test*)0x100000;//Structure size 20 0x14
//Suppose the value of p is 0x100000. What are the values of the expressions in the following table?
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
```

The first line: p+0x1 stores the structure type pointer, + 1 is the size of the type it points to!!!
Line 2: be careful! Be careful!!!, p is coerced into an unsigned long shape. It's not a pointer. Adding 1 is a constant plus 1. Don't confuse it
Line 3: turn p strong into
Unsigned int * plus 1 is the size of the type it points to!!!

## Strong turn to question 2

```int main()
{
int a[4] = { 1, 2, 3, 4 };
int *ptr1 = (int *)(&a + 1);
int *ptr2 = (int *)((int)a + 1);
printf("%x,%x\n", ptr1[-1], *ptr2);
return 0;
}
```

# Multidimensional arrays and multidimensional pointers

## 2D array:

Look, the address is linearly continuous and increasing
Drawing method of two-dimensional array:

3D array:

Understanding chain:
Array is defined as a collection with the same data element type. The characteristic is that any type can be saved in the array.
Can an array be saved in an array? The answer is yes!
In terms of understanding, we can even understand that all arrays can be regarded as "one-dimensional arrays"!
For two-dimensional arrays, we think that two-dimensional arrays can be regarded as "one-dimensional arrays", but the internal "elements" are also one-dimensional arrays
Then the internal one-dimensional array is arranged in memory and is "linearly continuous and increasing". If multiple one-dimensional arrays form another "one-dimensional array", the whole is also linearly continuous and increasing
This explains why the above addresses are continuous.
Once again, we think that a two-dimensional array can be regarded as a one-dimensional array whose internal elements are one-dimensional arrays

## Two dimensional array address + 1

Address of the entire array + 1

Address of the first element of the array + 1

First element address of the first element of the array + 1

## Two dimensional array exercise

```int main(){
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
printf("%d\n", sizeof(a[0] + 1));
printf("%d\n", sizeof(*(a[0] + 1)));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(*(a + 1)));
printf("%d\n", sizeof(&a[0] + 1));
printf("%d\n", sizeof(*(&a[0] + 1)));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a[3]));

return 0;
}
```

Explanation:

## Pointer and pointer subtraction

```int main(){
int a[10] = { 0 };
int*p = a;
int *r = &a[9];
printf("%d\n", r - p);//Pointer subtraction represents the number of elements experienced between pointers!!!
return 0;
}
```

Pithy formula: pointer subtraction represents the number of elements experienced between pointers!!!

## Exercise 2

```int main()
{
int a[5][5];
int(*p)[4];
p = a;
printf( "a_ptr=%p,p_ptr=%p\n", &a[4][2], &p[4][2]);
printf( "%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
return 0;
}
```

# Secondary pointer

```int main(){
int a = 10;
int *p = &a;
int **pp = &p;
p = 100; //what do you mean
*p = 100; //what do you mean
pp = 100; //what do you mean
*pp = 100; //what do you mean
**pp = 100; //what do you mean
}
```

Tags: C C++

Posted on Sat, 30 Oct 2021 03:41:04 -0400 by myfafa