Cprogramming 简明教程

Pointer vs Array in C

ArraysPointers 是 C 语言中的两个重要语言结构,在许多方面相互关联。在许多情况下,您使用指针执行的任务也可以在数组的帮助下执行。

Arrays and Pointers are two important language constructs in C, associated with each other in many ways. In many cases, the tasks that you perform with a pointer can also be performed with the help of an array.

但是,数组和指针之间存在某些概念差异。阅读本章以了解它们的差异以及各自的优缺点。

However, there are certain conceptual differences between arrays and pointers. Read this chapter to understand their differences and comparative advantages and disadvantages.

Arrays in C

在 C 程序中,数组是由相同类型元素组成的索引集合,这些元素存储在相邻的内存位置中。

In a C program, an array is an indexed collection of elements of similar type, stored in adjacent memory locations.

declare an array, 我们使用以下 syntax

To declare an array, we use the following syntax

data_type arr_name [size];

size 应为非负整数。例如:

The size should be a non-negative integer. For example −

int arr[5];

array can be initialized along with the declaration, with the elements given as a comma-separated list inside the curly brackets. Mentioning its size is optional.

The array can be initialized along with the declaration, with the elements given as a comma-separated list inside the curly brackets. Mentioning its size is optional.

int arr[] = {1, 2, 3, 4, 5};

数组中的每个元素都以一个从“0”开始的唯一 integral index, 为特征。在 C 语言中,数组的 lower bound 总为“0”,而 upper bound 为“size – 1”。

Each element in an array is characterized by a unique integral index, starting from "0". In C language, the lower bound of an array is always "0" and the upper bound is "size – 1".

Example of an Array

以下示例展示了如何遍历带有索引下标的数组 -

The following example shows how you can traverse an array with indexed subscripts −

#include <stdio.h>

int main (){

   /* an array with 5 elements */
   int arr[5] = {10, 20, 30, 40, 50};

   int i;

   /* output each array element's value */
   printf("Array values with subscripts: \n");

   for(i = 0; i < 5; i++){
      printf("arr[%d]: %d\n", i, arr[i]);
   }

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Array values with subscripts:
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50

Pointers in C

C 允许你访问一个变量的存储器位置,这个变量已由编译器随机分配。 address−of operator (&) 返回变量的地址。

C allows you to access the memory location of a variable that has been randomly allocated by the compiler. The address−of operator (&) returns the address of the variable.

variable 存储一个变量的地址称为指针。指针的类型必须与它存储地址的类型相同。

A variable that stores the address of another variable is called a pointer. The type of the pointer must be the same as the one whose address it stores.

为了与目标变量类型区分开来,指针名称前缀为星号 ( ). If we have an *int 变量,其指针声明为 "int *".

To differentiate from the target variable type, the name of the pointer is prefixed with an asterisk (). If we have an *int variable, its pointer is declared as "int *".

int x = 5;
int *y = &a;

Note: 对于数组,其第 0 个元素的地址分配给指针。

Note: In case of an array, the address of its 0th element is assigned to the pointer.

int arr[] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];

事实上,数组本身的名称解析为第 0 个元素的地址。

In fact, the name of the array itself resolves to the address of the 0th element.

pointer vs array

因此,我们同样可以编写 -

Hence, we can as well write −

int *ptr = arr;

由于数组元素放置在相邻的存储器位置中,每个下标的地址递增 4(对于 int 数组),我们可以使用此功能借助指针来遍历数组元素。

Since the elements of an array are placed in adjacent memory locations and the address of each subscript increments by 4 (in case of an int array), we can use this feature to traverse the array elements with the help of pointer as well.

Example of a Pointer

以下示例展示了如何遍历带有指针的数组 -

The following example shows how you can traverse an array with a pointer −

#include <stdio.h>

int main (){

   /* an array with 5 elements */
   int arr[5] = {10, 20, 30, 40, 50};
   int *x = arr;

   int i;

   /* output each array element's value */
   printf("Array values with pointer: \n");

   for(i = 0; i < 5; i++) {
      printf("arr[%d]: %d\n", i, *(x+i));
   }

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Array values with pointer
arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50

Difference between Arrays and Pointers in C

下表突显了数组和指针之间的重要区别 -

The following table highlights the important differences between arrays and pointers −

Array

Pointer

It stores the elements of a homogeneous data type in adjacent memory locations.

It stores the address of a variable, or an array

An array is defined as a collection of similar datatypes.

A pointer is a variable that stores address of another variable.

The number of variables that can be stored is decided by the size of the array.

A pointer can store the address of only a single variable.

The initialization of arrays can be done while defining them.

Pointers cannot be initialized while defining them.

The nature of arrays is static.

The nature of pointers is dynamic.

Arrays cannot be resized according to the user’s requirements.

Pointers can be resized at any point in time.

The allocation of an array is done at compile time.

The allocation of the pointer is done at run time.