[key and difficult points of C language] C language pointer

Posted by Hexen on Sat, 23 Oct 2021 05:32:31 +0200

Pointer is a key point in C language. The basic use of pointer understanding is not within the scope of this article. Readers can read the articles of other bloggers in the station, which are well written. This paper mainly summarizes some key points and difficulties in pointer, and the most important thing is the relationship with array

1: Introduction to pointer

If the reader can deeply understand the meaning of the following statements, the pointer can even meet the entry standard

//first group
int a=10;
int* p=&a;
//Group 2
p=10;
int* q=p;
//Group 3
*p=10;
int b=*p;
  • The first group: define an integer variable a and assign a value of 10, then define a pointer variable p and initialize it with the address of A
  • The second group: assign 10 to the pointer variable p, at this time, the pointer P points to an address of 10, and then assign the content of P to a pointer variable q of the same type
  • The third group: * p means dereference, change the content in the space pointed to by p to 10, and then assign it to variable b

2: Getting started with arrays

(1) Memory space layout of array

The array applies for space as a whole, and then takes the space with the lowest address as the a[0] element


This is why we use + + when accessing arrays or accessing arrays with pointers. The essence is that the elements are arranged according to the increasing direction of the address

(2) Distinguish between & arr [0] and & arr

First, we need to find out what the pointer + 1 means. Look at the following code

int main()
{
	char* c = NULL;
	short* s = NULL;
	int* i = NULL;
	double* d = NULL;

	printf("%d\n", c);
	printf("%d\n\n", c + 1);

	printf("%d\n", s);
	printf("%d\n\n", s + 1);

	printf("%d\n", i);
	printf("%d\n\n", i + 1);

	printf("%d\n", d);
	printf("%d\n\n", d + 1);

}

The operation results are as follows

Therefore, pointer + 1 actually adds the size of the pointer type

Back to the point

int main()
{
	char arr[10] = { 0 };
	printf("%p\n", &arr[0]);
	printf("%p\n", &arr[0]+1);

	printf("%p\n", &arr);
	printf("%p\n", &arr+1);
}
  • &Arr, sizeof(arr): arr represents the entire array,
  • &Arr [0]: represents the address of the first element of the array, because [] has higher priority
  • &Arr [0] + 1: because it is an array of char type, it is + 1
  • &Arr + 1: This is the address of the array, which can be understood across the entire array

In addition, when the array name is used as an R-value, it represents the address of the first element of the array, which is essentially equivalent to & arr [0]; However, array names cannot be used as lvalues. Those that can be used as lvalues must have space and can be modified

3: Relationship between pointer and array

Pointers have nothing to do with arrays. They are two completely different sets of specifications, but they have intersection in operation

(1) Accessed as a pointer and as an array

There are two forms of saved strings in C language, as follows

int main()
{
	char* str = "abcdef";//The str pointer is saved on the stack, "abcdef" is in the character constant area and cannot be modified
	char arr[] = "abcdef";//The entire array is saved on the stack and can be modified
}

1: The pointer is accessed as a pointer and a subscript, respectively

int len = strlen(str);
for (int i = 0; i < len; i++)
{
	printf("%c ", *(str + i));//Access by pointer
	printf("%c ", str[i]);//Access as an array
}
printf("\n");

1: Access the array as a pointer and a subscript, respectively

int len = strlen(arr);
for (int i = 0; i < len; i++)
{
	printf("%c ", *(arr+i));//Access by pointer
	printf("%c ",arr[i]);//Access as an array
}
printf("\n");

When pointers and arrays point to or represent a space, the access methods can be interconnected and similar, but note that they are not the same thing

(2) Why is C designed like this?

We know that this method can be used when passing array parameters

void Show(int arr[], int num)
{
	for (int i = 0; i < num; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int arr[] = { 1,2,3,4,5 };
	int num = sizeof(arr) / sizeof(arr[0]);
	Show(arr, num);
}

The sizeof(arr)/sizeof(arr[0]) statement cannot be put into the function, otherwise only one element will be printed. I believe beginners have made a lot of mistakes here.

One of the advantages of pointer and array interworking is here, because if they are not interworking, a huge thing like array will waste a lot of extra space when transmitting parameters. Therefore, simply reduce its dimension into a pointer when transmitting parameters and directly let the pointer access it

In this case, the int arr in the formal parameter can actually be written as int* arr

void Show(int* arr, int num)
{
	for (int i = 0; i < num; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main()
{
	int arr[] = { 1,2,3,4,5 };
	int num = sizeof(arr) / sizeof(arr[0]);
	Show(arr, num);
}

Therefore, when passing parameters, all arrays will be reduced to pointers - pointers to their internal types. Of course, one-dimensional arrays are pointers to corresponding data types, and two-dimensional arrays are array pointers to one-dimensional arrays

Topics: C