table of Contents
Last talk about pointer links:
The fifth part of the pointer (const modified pointer, multi-level pointer) ---- 2021.2.22
Pointer array
When we touch pointers, we often come into contact with the two concepts of pointer arrays and array pointers. Let's first talk about the concept and usage of pointer arrays today. As for array pointers, we will mention them in a later article.
First of all, before talking about pointer arrays, I think it is necessary to introduce the concept of ordinary arrays, that is, integer arrays.
Integer array : is an array, each element of the array is an integer.
Then we can introduce pointer arrays from the concept of integer arrays.
Pointer array : is an array, each element of the array is a pointer.
Then under what conditions did we propose the pointer array?
Because as shown in the following code, we define three integer variables a
, b
and c
. And assign values to 10
, 20
and respectively 30
. Then we need to define three pointers to save the addresses of its three variables, namely *p1
, *p2
and *p3
.
int main()
{
int a = 10;
int b = 20;
int c = 30;
int* p1 = &a;
int* p2 = &b;
int* p2 = &c;
}
We can do this normally, but what if we imagine that there are many parameters that need to save the address? Is it very trivial to use this method, then some people have proposed the concept of pointer array, which solves this problem very well. Let's see how to use it.
int main()
{
int a = 10;
int b = 20;
int c = 30;
//需求: 数组中的每一个元素都是指针(地址)
int* num[3] = {
&a,&b,&c };
printf("%d\n",sizeof(num));
for (int i = 0;i < sizeof(num) / sizeof(num[0]);i++)
{
printf("%d ", *num[i]);
}
}
analyse as below:
- We define three integer variables
a
,b
andc
. And assign values10
,20
and respectively30
. At this point we want to define a pointer array to store the addresses of these three integer variables, then we need to know whatnum
the data type of the pointer array is. For example, becausea
the data type of a variable is aint
type, it can be seen from the previous articles that&
the data type of the variable will be added to the final data type,“ * ”
and then the data type of the pointer array we need to define is theint *
type. printf("%d\n",sizeof(num));
This sentence actuallynum
prints the byte size of the array type of the pointer array . The normal result is12
. Because we mentioned in the previous articles, no matter what data type pointer is, the final byte size is all4
. There are three variables of the same data type in the pointer array, so it is normal3 x 4 =12
, and the result is as follows:
- So if we want to print element array of pointers that we first need to know there are several elements in the array of pointers, the
sizeof(num)
byte array of pointers to the total size of the elements, and the data type of the elements of the array of pointers are the same , Then we only need to extract a certain element in the array. Here, take the first element as an example, that issizeof(num[0])
, the size of the array element can be obtained by dividing the two formulas. In this example, is3
.
Then finally print the pointer content, we need to“ * ”
operate. - In order to let everyone better understand, I specially drew a picture for everyone to watch.
So at this time we want to define a pointer to store the address of the first element of the array num, how should we define it?
This will use the multi-level pointers we mentioned in the previous articles.
For example, if num[0]
the data type is a int*
type, then we need to define a pointer variable to store its address. Then k
the data type of our newly defined pointer variable should be one level higher than the original “ * ”
.
Code first! ! !
int main()
{
int a = 10;
int b = 20;
int c = 30;
//需求: 数组中的每一个元素都是指针(地址)
int* num[3] = {
&a,&b,&c };
printf("%d\n", sizeof(num));
int** k = num;
for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
{
printf("%d ", **(k + i));
}
}
The analysis of the above code is as follows:
- From the previous analysis, we can see that the data type of our newly defined pointer variable k should be a
int**
type under normal circumstances , and our purpose is to savenum
the address of the first element of the array , which isnum
equivalent to&num[0]
. So the final definition isint** k = num;
- Then we want
k
to obtain the variable through the pointer variablea
,b
andc
the content, that is10
,20
and30
. Then takea
the content of the variable as an example. We need to derive level by level at this time. Because itk
is a pointer variable, that is, the pointer variablek
holds thenum
address of the first element of the array, thatk = &num[0];
is, we wantk
to getnum
the content of the first element of the array through the pointer variable , that is,“ * ”
operate on the pointer variable k . So even*k = &a;
if you perform another“ * ”
operation, you can extract the contents of the memory space ofnum[0]
the corresponding variable pointed to by the pointer variablea
, that is**k = 10;
, you can finallyk
geta
the contents of the variable through the secondary pointer . Achieved our goal. The other is the same. - The final operation result is shown below.
- At the same time, in order to let everyone better understand, I specially drew a picture for everyone to watch.
Pointer as a formal parameter of a function
Code first! ! !
void swap(int x, int y)
{
int k = x;
x = y;
y = k;
printf("x=%d y=%d\n", x, y);
}
int main()
{
int a = 10;
int b = 20;
swap(a,b);
printf("a=%d b=%d\n", a, b);
system("pause");
return 0;
}
Analysis of the above code:
-
We define the sum of two integer variables
a
andb
assign the values to the10
sum respectively20
. By then we eventually want toswap
functiona
andb
carry out exchange value. Thata
is20
,b
the value of the final effect is , the value of10
. The results of the operation are as follows:
-
We can see from the above results that the final
a
sumb
value was not successfully exchanged. Why is this? In order to let everyone better understand, I specially drew a picture for everyone to watch.
Beginninga
value10
,b
a value20
, then entering theswap
function parameterx
value10
,y
value20
. So in this case defines an intermediate integer variablek
, for temporarily storingx
the value, and finally throughk
to bex
the value assignedy
, i.e. finally reachingx
andy
values are replaced by each effect, but when the exitswap
after the function, in fact,x
andy
the equivalent Temporary variables will not be stored in virtual memory, that is, the finala
sumb
value still has no real change, thata
is10
,b
the final result is the same as the final result in the above figure, and the value is the value of20
. So how do weswap
realize the exchange of the values ofa
and through functionsb
? At this time the pointer comes in handy! -
The modified
swap
code is as follows:
void swap(int *x, int *y)
{
int k = *x;
*x = *y;
*y = k;
printf("x=%d y=%d\n", *x, *y);
}
int main()
{
int a = 10;
int b = 20;
swap(&a,&b);
printf("a=%d b=%d\n", a, b);
system("pause");
return 0;
}
Also in order to let everyone better understand, I specially drew a picture for everyone to watch.
The analysis of the above figure is as follows:
First of all, we can clearly see swap
that the interface parameter of the incoming function is the &a
sum &b
, which is the address of the a
sum b
. Then the pointer variable x
and the pointer variable y
point to the variable a
and the variable respectively b
. Then the exchange swap
of the x
sum in the function y
actually directly manipulates the content of the variable a
sum b
in disguise , that is, finally a
becomes 20
, b
becomes 10
, successfully exchanged through the pointer, and the final operation result is as follows:
Concluding remarks
If you think this article is good, remember to like it and support it! ! !