Understanding of several pointers

There are many pointers

int* arr=&a; integer pointer
char* ch=&b; character pointer

These are pointers to integers and character types, arr stores integer addresses, and ch stores character addresses. There is also a special pointer.

const char* ch=“abcdef”;

This is a constant string and cannot be modified.
As we all know, the array name represents the address of the first element

arr-the address of the first element
& arr-the address of the array

There are differences, keep in mind.

char arr1[]="abcdef";
char arr2[]="abcdef";

Determine whether arr1 is equal to arr2.
The elements stored in the two arrays are the same, but two arrays are created in space, namely arr1 and arr2. Moreover, the addresses of the two arrays in memory are not the same. However, we know that the array name represents the address of the first element, and the addresses of the two arrays in the memory are different, the address of the first element is also different. That is not equal.
This is a bit different below

const char* p1="abcdef";
const char* p2="abcdef";

see this. Two identical string constants cannot be modified.
And two identical string constants only need to store one copy in the memory. But p1 and p2 are independent of each other, and two spaces are created in the memory and named p1 and p2 respectively. The pointers point to the same address. So they are equal.

Pointer array and array pointer

Let's discuss pointer arrays first. The
pointer array is an array, but the stored elements are pointers, which are addresses.

int* arr[4]; an array of pointers to integers
char* pch[5]; an array of pointers to characters

int a=10;
int b=20;
int c=30;
int* arr[3]={
    
    &a,&b,&c};//int*是代表元素的类型,指针类型。

You can also store pointers to arrays

int arr1[]={
    
    1,2,3};
int arr2[]={
    
    2,3,4};
int arr3[]={
    
    3,4,5};
int* pch[]={
    
    arr1,arr2,arr3};
//数组名就是数组首元素的地址
for(i=0;i<3;i++)
{
    
    
	for(j=0;j<3;j++)
	{
    
    
		printf("%d",*(pch[i]+j);
		printf("%d",*(*(pch+i)+j));
	}
}
  • ( (pch+i)+j)
    pch can be regarded as an array, each element is arr1.arr2, arr3. Use
    (pch+i) dereference to extract the name of each element. pch+0, pch+1, pch+2, is the array skip. The array name is just an address, and the address stored in pch is also an address. To extract it, get the array name, enter the arr array and start extracting elements.

Let's look at the array pointer. It
is a pointer, a pointer to an array—the address of the array.

int arr[10];
arr-first element address
&arr[0] -first element address &arr-
array address

[] Has a higher priority than *.

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

The array pointer stores the address of the array arr. (*P) indicates that it is a pointer, and arr is an array of 10 elements. We must first understand what each represents

int arr[5]; arr is an integer array, and each element stored is int integer.
int *arr[5]; arr is an array of pointers, and each element stored is a pointer type, which is an address.
int (*p)[5]=&arr, is an array pointer, *p indicates a pointer, [5] indicates that there are 5 elements in the array arr stored in the pointer p, and int indicates that 5 elements are of int type .

one left

int *( p)[5]=&arr is the
same as above, except that it is of
type int , which means that the pointer type stored in the arr array is also an address.

Like the storage address, extract the address inside, *p is equal to the array name, which is the first element *p=arr,
(*p)[i]==arr[i], just like

int* p=&arr;
p==arr
arr[i]=p[i];

The other one is hard to understand

int (*p[10])[5]=int ( * ----- )[5]
--------------------------p[10]

In this way, p is first combined with [], which is an array

int(*p)[10]; p is an array pointer, which points to an array with 10 Int elements.

It can be compared with an array of pointers.
p is an array, and p is before [] combined with *. The combined description is an array that stores 10 pointers, and the outside int (*) [5] means that each pointer points to a storage of 5 int types Array.
To sum up, p is an array of 10 elements, and each element is a pointer to an array with 5 elements of int type.
It's really hard to understand, and I still have to learn more.

Guess you like

Origin blog.csdn.net/weixin_52199109/article/details/110789142