C language pointers-pointers and arrays

The best time to plant a tree is ten years ago, followed by now.

Pointers and arrays

One, pointer operation

The pointer can perform three operations:

1. Pointer plus integer :

If the pointer p points to the array a[i], then the pointer p + j points to a[i + j] (provided that a[i + j] exists!)

2.Pointer minus integer:

If the pointer p points to the array a[i], then the pointer p-j points to a[i-j] (provided that a[i-j] exists!)

3. Adding and subtracting two pointers (the two pointers must point to the same variable!!! Otherwise the operation is meaningless! )

When two pointers are subtracted, the result is the distance of the pointer in the memory, which can be measured by the number of array elements, so if the pointer p points to a[ i] and q points to a[ j ], then p-q is equal to i-j.

Pointer comparison: Pointers can be compared using symbols such as> <= >= <= == != (also only when the pointer points to the same array, pointer comparison using relational operators is meaningful!)

Two, pointers are used for array processing

Due to the arithmetic operations of pointers, we can access the elements of the array by incrementing and decrementing pointers.

#include<stdio.h>

int main()
{
	int a[10] = {0,1,2,3,4,5,6,7,8,9};
	int *p;
	for(p = &a[0];*p < a[10];p++)
	{
		printf("%d\n",*p);
	}
	
	
	return 0;
}

Using array subscripts can write loops without pointers. There is an argument that using pointers can save time, but for current compilers, loops using subscripts are actually optimized automatically, that is, loops relying on subscripts will produce Better code (the big guy said [^.^])

 

*Combined with ++:

#include<stdio.h>

int main()
{
	int a[10] = {0,1,2,3,4,5,6,7,8,9};
	int *p = &a[0];// *p = a;
	while (*p < a[10])
	{
		printf("%d\n",*p++);
	}	
	return 0;
}

Of course there are *++p, (*p)++

Three, use the array name as a pointer

1. You can use the array name as a pointer to the first element of the array . For example, int a[10]; *a = 7; this is to assign 7 to the a[0] element. Normally, a + i is equivalent to &a[i], and *(a + i) is equivalent to a[i].

2. The array variable itself expresses the address, so use the array to initialize the pointer without taking the address symbol, int a[10]; int *p = a;. But each element of the array expresses a variable, and the address character & needs to be taken. 

3. [] can also be done for pointers, p[0] is equivalent to a[0]. *It can also be done on arrays, *a is equivalent to a[0].

Note: When the array variable is a pointer of const type, it cannot be assigned!

 

4. Array as a function parameter

Let's look at an interesting thing (It's amazing!):

#include<stdio.h>
void test1(int a[]);

int main()
{
	int a[5] = {0,1,2,3,3};
	printf("main中sizeof(a[]) = %lu\n",sizeof(a));
	
	test1(a);
	
	return 0;
}

void test1(int a[])
{
	printf("函数中sizeof(a[]) = %lu\n",sizeof(a));
}

The results of the operation are as follows:

amazing

In the array passed in by sizeof in function test1, we find that its byte size is 8! Instead of the byte size 20 of the array itself! Interesting hahahaha QAQ. This is because the array passed in as a parameter is a pointer! That is, the array variable we said before is a special pointer. The array name is always treated as a pointer when passed to the function .

This is of great significance to us! :

(1). When passing ordinary variables to the function, the value of the variable will be copied, and any changes to the corresponding formal parameters will not affect the variable. We can change the array as the actual parameter (unless it is const)!

(2). There is no relationship between the time of passing the array to the function and the size of the array half a dime! (Compiler: "I didn't copy your array at all, I didn't expect it! Hahaha")

(3). We can declare the array parameters as pointers when needed. (The compiler's declaration of arrays and pointers is completely the same)

Note: For formal parameters, declaring as an array is the same as declaring as a pointer; but for variables, declaring as an array is different from declaring as a pointer!

5. Use the pointer name as the array name (the following code)

#include<stdio.h>

int main()
{
	int a[10],*p = a, i = 0;
	
	for(i = 0;i < 10;i++)
	{
		*p++ = i;
		printf("%d\n",a[i]);
	}
	
	return 0;
}

Pointer name as function name

Fourth, pointers and multidimensional arrays

int a[4][5] = {
   
   {0,1,2,3,4},{10,11,12,13,14},{20,21,22,23,24},{30,31,32,33,34}};

This can be understood as an array containing four row elements: a[0], a[1], a[2], a[3], you can put a[0]

The other thing to remember is the address of the first element of the array name. There is a saying that a is not the address of a[0][0], but I tried it, and the result is that they have the same address, so I can summarize it in one sentence: the array name is the address of the first element.

Row pointer: Each row of a two-dimensional array is regarded as a one-dimensional array, and a pointer to the one-dimensional array of each row.

Process the rows of a multidimensional array:

#include<stdio.h>

int main()
{
	int a[4][5] = {
   
   {0,1,2,3,4},{10,11,12,13,14},{20,21,22,23,24},{30,31,32,33,34}};
	int *p,i = 0,j = 4;
	i = 3;
	for (p = a[i];p < a[i] + j;p++)//p = a[i]给p初始化.p < a[i] + j 中a[i]+j表示的是a[i][j] 
	{
		*p = 0;
	}
	
	printf("%d\n",a[i][2]);//输出验证第i行第2列是否为0 
	return 0;
}

After running p++ once, it is equivalent to p +1, which is equivalent to a[i] +1, which is equivalent to a[i][1]

Processing the columns of a multidimensional array:

#include<stdio.h>

int main()
{
	int a[4][5] = {
   
   {0,1,2,3,4},{10,11,12,13,14},{20,21,22,23,24},{30,31,32,33,34}};
	int (*p)[5],i = 2;//(*p)的括号不可省略![5]这个表示的是一维数组的长度,不可省略!定义了一个行指针,其宽度为5(即二维数组的列数为5) 
	for ( p = &a[0]; p < &a[4];p++)//p=&a[0]是把第0行那个一行的一维数组的地址交给p这个行指针  &a[4]相当于a+4. p=&a[0]相当于p=a 
	{
		(*p)[i] = 0;//(*p)这里代表着a的一整行 
	} 
	printf("%d\n",a[2][i]);//输出验证第2行第2列是否为0 
	return 0;
}

 

 

 

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/115032443