Cprogramming 简明教程
Pointers and Arrays in C
在 C 编程中,数组和指针的概念扮演着非常重要的角色。两者之间也有密切的联系。本章将详细解释 C 编程中数组和指针之间的关系。
In C programming, the concepts of arrays and pointers have a very important role. There is also a close association between the two. In this chapter, we will explain in detail the relationship between arrays and pointers in C programming.
Arrays in C
C 中的数组是存储在连续内存块中且数据类型均相同的一系列元素。数组大小是方括号中的一个整数,放在数组名称前面。
An array in C is a homogenous collection of elements of a single data type stored in a continuous block of memory. The size of an array is an integer inside square brackets, put in front of the name of the array.
Declaring an Array
要声明一个数组,使用以下内容:
To declare an array, the following syntax is used −
data_type arr_name[size];
数组中的每个元素都有一个唯一的递增索引,从“0”开始。可以用不同的方式声明和初始化数组。
Each element in the array is identified by a unique incrementing index, starting from "0". An array can be declared and initialized in different ways.
可以声明一个数组,然后在代码中根据需要稍后初始化它。:
You can declare an array and then initialize it later in the code, as and when required. For example −
int arr[5];
...
...
a[0] = 1;
a[1] = 2;
...
...
还可以同时声明和初始化一个数组。要存储的值作为一个以逗号分隔的列表放在大括号中。
You can also declare and initialize an array at the same time. The values to be stored are put as a comma separated list inside curly brackets.
int a[5] = {1, 2, 3, 4, 5};
当在声明时初始化一个数组时,可以不指定其大小,因为编译器会自动计算大小。因此,以下语句也有效:
Note: When an array is initialized at the time of declaration, mentioning its size is optional, as the compiler automatically computes the size. Hence, the following statement is also valid −
int a[] = {1, 2, 3, 4, 5};
Example: Lower Bound and Upper Bound of an Array
数组中的所有元素都有一个从“0”开始的位置索引。数组的开始边界始终为“0”,而上限为。我们可以使用此属性在循环中遍历、赋值或读取数组下标中的输入。
All the elements in an array have a positional index, starting from "0". The lower bound of the array is always "0", whereas the upper bound is "size − 1". We can use this property to traverse, assign, or read inputs into the array subscripts with a loop.
请查看以下示例:
Take a look at this following example −
#include <stdio.h>
int main(){
int a[5], i;
for(i = 0; i <= 4; i++){
scanf("%d", &a[i]);
}
for(i = 0; i <= 4; i++){
printf("a[%d] = %d\n",i, a[i]);
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
a[0] = 712952649
a[1] = 32765
a[2] = 100
a[3] = 0
a[4] = 4096
Pointers in C
指针是一个存储另一个变量地址的变量。在 C 中,符号用于作为。此运算符返回的值被分配给一个指针。
A pointer is a variable that stores the address of another variable. In C, the symbol (&) is used as the address-of operator. The value returned by this operator is assigned to a pointer.
要将一个变量声明为指针,需要在名称前放置一个星号 (*)。另外,指针变量的类型必须与它存储的地址的变量类型相同。
To declare a variable as a pointer, you need to put an asterisk (*) before the name. Also, the type of pointer variable must be the same as the type of the variable whose address it stores.
在此代码段中,是一个存储整数变量地址的整数指针:
In this code snippet, "b" is an integer pointer that stores the address of an integer variable "a" −
int a = 5;
int *b = &a;
对于数组,可以将它的第 0 个元素的地址分配给指针。
In case of an array, you can assign the address of its 0th element to the pointer.
int arr[] = {1, 2, 3, 4, 5};
int *b = &arr[0];
在 C 中,数组的名称自身可解析为它第 0 个元素的地址。这意味着在上述情况下,我们可以使用作为等价于。
In C, the name of the array itself resolves to the address of its 0th element. It means, in the above case, we can use "arr" as equivalent to "&[0]":
int *b = arr;
Example: Increment Operator with Pointer
与普通的数字变量(增量运算符“++”将其值增加 1)不同,与指针一起使用的增量运算符将其值增加其数据类型的。
Unlike a normal numeric variable (where the increment operator "++" increments its value by 1), the increment operator used with a pointer increases its value by the sizeof its data type.
因此,当一个指针被递增时,它会增加 4。
Hence, an int pointer, when incremented, increases by 4.
#include <stdio.h>
int main(){
int a = 5;
int *b = &a;
printf("Address of a: %d\n", b);
b++;
printf("After increment, Address of a: %d\n", b);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Address of a: 6422036
After increment, Address of a: 6422040
The Dereference Operator in C
在 C 中,返回指向的指针所指向地址处存储的值。
In C, the "" symbol is used as the *dereference operator. It returns the value stored at the address to which the pointer points.
因此,以下语句返回“5”,它是存储在变量中的值,变量指向的变量。
Hence, the following statement returns "5", which is the value stored in the variable "a", the variable that "b" points to.
int a = 5;
int *b = &a;
printf("value of a: %d\n", *b);
在字符指针的情况下,它会增加 1;在指针的情况下,它将增加 8;在类型的情况下,它会增加该结构类型的大小。
Note: In case of a char pointer, it will increment by 1; in case of a double pointer, it will increment by 8; and in case of a struct type, it increments by the sizeof value of that struct type.
Example: Traversing an Array Using a Pointer
我们可以借助指针遍历该数组元素以使用该指针属性。
We can use this property of the pointer to traverse the array element with the help of a pointer.
#include <stdio.h>
int main(){
int arr[5] = {1, 2, 3, 4, 5};
int *b = arr;
printf("Address of a[0]: %d value at a[0] : %d\n",b, *b);
b++;
printf("Address of a[1]: %d value at a[1] : %d\n", b, *b);
b++;
printf("Address of a[2]: %d value at a[2] : %d\n", b, *b);
b++;
printf("Address of a[3]: %d value at a[3] : %d\n", b, *b);
b++;
printf("Address of a[4]: %d value at a[4] : %d\n", b, *b);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
address of a[0]: 6422016 value at a[0] : 1
address of a[1]: 6422020 value at a[1] : 2
address of a[2]: 6422024 value at a[2] : 3
address of a[3]: 6422028 value at a[3] : 4
address of a[4]: 6422032 value at a[4] : 5
Points to Note
需要注意的是:
It may be noted that −
-
"&arr[0]" is equivalent to "b" and "arr[0]" to "*b".
-
Similarly, "&arr[1]" is equivalent to "b + 1" and "arr[1]" is equivalent to "(b + 1)".*
-
Also, "&arr[2]" is equivalent to "b + 2" and "arr[2]" is equivalent to "(b+2)".*
-
In general, "&arr[i]" is equivalent to "b + I" and "arr[i]" is equivalent to "(b+i)".*
Example: Traversing an Array using the Dereference Operator
我们可以使用此属性并使用一个循环来遍历具有 dereference operator. 的数组
We can use this property and use a loop to traverse the array with the dereference operator.
#include <stdio.h>
int main(){
int arr[5] = {1, 2, 3, 4, 5};
int *b = arr;
int i;
for(i = 0; i <= 4; i++){
printf("a[%d] = %d\n",i, *(b+i));
}
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
您还可以在每次迭代中增加指针并获得相同的结果 −
You can also increment the pointer in every iteration and obtain the same result −
for(i = 0; i <= 4; i++){
printf("a[%d] = %d\n", i, *b);
b++;
}
在 C 中,数组和指针的概念密切相关。因为指针直接处理内存地址,所以您可使用指针来提高程序的效率。指针还可用于处理多维数组。
The concept of arrays and pointers in C has a close relationship. You can use pointers to enhance the efficiency of a program, as pointers deal directly with the memory addresses. Pointers can also be used to handle multi−dimensional arrays.