Cprogramming 简明教程

Array of Pointers in C

What is an Array of Pointers?

就像整数数组保存一组整型变量, array of pointers 将保存指针类型的变量。这意味着指针数组中的每个变量都是一个指针,指向另一个地址。

Just like an integer array holds a collection of integer variables, an array of pointers would hold variables of pointer type. It means each variable in an array of pointers is a pointer that points to another address.

array 的名称可以用作 pointer ,因为它包含数组的第一个元素的地址。如果我们将数组的地址存储在另一个指针中,那么有可能使用 pointer arithmetic 操作数组。

The name of an array can be used as a pointer because it holds the address to the first element of the array. If we store the address of an array in another pointer, then it is possible to manipulate the array using pointer arithmetic.

Create an Array of Pointers

要在 C 语言中创建指针数组,你需要按指针声明的方式声明一个指针数组。使用数据类型,然后是一个星号,后跟一个标识符(指针变量数组名),以及包含数组大小的脚本 ([])。

To create an array of pointers in C language, you need to declare an array of pointers in the same way as a pointer declaration. Use the data type then an asterisk sign followed by an identifier (array of pointers variable name) with a subscript ([]) containing the size of the array.

在指针数组中,每个元素都包含指向特定类型的指针。

In an array of pointers, each element contains the pointer to a specific type.

Example of Creating an Array of Pointers

以下示例演示了如何创建和使用指针数组。在这里,我们声明了三个整数变量,并为访问和使用它们,我们创建了一个指针数组。借助指针数组,我们可以打印出这些变量的值。

The following example demonstrates how you can create and use an array of pointers. Here, we are declaring three integer variables and to access and use them, we are creating an array of pointers. With the help of an array of pointers, we are printing the values of the variables.

#include <stdio.h>

int main() {
  // Declaring integers
  int var1 = 1;
  int var2 = 2;
  int var3 = 3;

  // Declaring an array of pointers to integers
  int *ptr[3];

  // Initializing each element of
  // array of pointers with the addresses of
  // integer variables
  ptr[0] = &var1;
  ptr[1] = &var2;
  ptr[2] = &var3;

  // Accessing values
  for (int i = 0; i < 3; i++) {
    printf("Value at ptr[%d] = %d\n", i, *ptr[i]);
  }

  return 0;
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

有时,我们可能想维护一个数组,该数组可以存储指向“Int”或“Char”或任何其他可用数据类型的指针。

There may be a situation when we want to maintain an array that can store pointers to an "int" or "char" or any other data type available.

An Array of Pointers to Integers

以下是整数指针数组的声明:

Here is the declaration of an array of pointers to an integer −

int *ptr[MAX];

它将 ptr 声明为 MAX 整数指针数组。因此, ptr 中的每个元素都保存着一个 int 值的指针。

It declares ptr as an array of MAX integer pointers. Thus, each element in ptr holds a pointer to an int value.

Example

以下示例使用三个存储在指针数组中的整数,如下所示:

The following example uses three integers, which are stored in an array of pointers, as follows −

#include <stdio.h>

const int MAX = 3;

int main(){

   int var[] = {10, 100, 200};
   int i, *ptr[MAX];

   for(i = 0; i < MAX; i++){
      ptr[i] = &var[i]; /* assign the address of integer. */
   }

   for (i = 0; i < MAX; i++){
      printf("Value of var[%d] = %d\n", i, *ptr[i]);
   }

   return 0;
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

An Array of Pointers to Characters

您还可以使用一个指向字符的指针数组来存储 strings 列表,如下所示:

You can also use an array of pointers to character to store a list of strings as follows −

#include <stdio.h>

const int MAX = 4;

int main(){

   char *names[] = {
      "Zara Ali",
      "Hina Ali",
      "Nuha Ali",
      "Sara Ali"
   };

   int i = 0;

   for(i = 0; i < MAX; i++){
      printf("Value of names[%d] = %s\n", i, names[i]);
   }

   return 0;
}

Output

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali

An Array of Pointers to Structures

当您拥有 structures 列表并想要使用指针管理它时。您可以声明一个 array of structures 来访问和操作结构列表。

When you have a list of structures and want to manage it using a pointer. You can declare an array of structures to access and manipulate the list of structures.

Example

以下示例演示了如何使用结构的指针数组。

The below example demonstrates the use of an array of pointers to structures.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Declaring a structure
typedef struct {
  char title[50];
  float price;
} Book;

const int MAX = 3;
int main() {
  Book *book[MAX];

  // Initialize each book (pointer)
  for (int i = 0; i < MAX; i++) {
    book[i] = malloc(sizeof(Book));
    snprintf(book[i]->title, 50, "Book %d", i + 1);
    book[i]->price = 100 + i;
  }

  // Print details of each book
  for (int i = 0; i < MAX; i++) {
    printf("Title: %s, Price: %.2f\n", book[i]->title, book[i]->price);
  }

  // Free allocated memory
  for (int i = 0; i < MAX; i++) {
    free(book[i]);
  }

  return 0;
}

Output

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Title: Book 1, Price: 100.00
Title: Book 2, Price: 101.00
Title: Book 3, Price: 102.00