如何在 C 中创建指针数组?

hel*_*elp 5 c arrays pointers

如何创建一个指针数组,其中每个元素都保存一个指向其他值的指针

例如,如果我有

int** arr[5] = {0xbfjeabfbfe,0x...}; //is it the right way to do it?
Run Code Online (Sandbox Code Playgroud)

数组类型意味着什么void?喜欢void **newArray[5];

假设我想使用 malloc 或 calloc 为指针数组动态分配内存!语法是什么?

Dav*_*ica 7

如何在 C 中创建指针数组?

要在 C 中创建指针数组,您有一个选择,您可以声明:

  type *array[CONST];  /* create CONST number of pointers to type */
Run Code Online (Sandbox Code Playgroud)

使用 C99+,您可以创建指针的可变长度数组(VLA),例如

  type *array[var];   /* create var number of pointers to type */
Run Code Online (Sandbox Code Playgroud)

该标准在C11 标准 - 6.7.6.2 数组声明符中定义了两者,并在C11 标准 - 6.5.2.1 数组下标中讨论了下标。

使用指针数组的简短示例,将指向 2D 数组中每一行的指针分配给指向 的指针数组int,例如

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

#define COL 3
#define MAX 5

int main (void) {

    int arr2d[MAX][COL] = {{ 0 }},  /* simple 2D array */
        *arr[MAX] = { NULL },       /* 5 pointers to int */
        i, j, v = 0;

    for (i = 0; i < MAX; i++) {     /* fill 2D array */
        for (j = 0; j < COL; j++)
            arr2d[i][j] = v++;
        arr[i] = arr2d[i];          /* assing row-pointer to arr */
    }

    for (i = 0; i < MAX; i++) {     /* for each pointer */
        for (j = 0; j < COL; j++)   /* output COL ints */
            printf (" %4d", arr[i][j]);
        putchar ('\n');
    }
}
Run Code Online (Sandbox Code Playgroud)

使用/输出示例

$ ./bin/array_ptr2int_vla
    0    1    2
    3    4    5
    6    7    8
    9   10   11
   12   13   14
Run Code Online (Sandbox Code Playgroud)

C 的另一个基本原理是指针到指针,但它不是“数组”,尽管它通常被称为“动态数组”并且可以模拟数组进行分配和索引。“数组”和指针集合之间的区别在于,对于数组,所有值都保证在内存中是连续的——而指针集合及其引用的内存位置则没有这样的保证。

那么声明了什么int **arr[CONST]

在您的问题中,您提出了 的声明 int** arr[5] = {0xbfjeabfbfe,0x...};,那么它声明了什么?你正在宣布某事的五,但是什么?您正在声明五个指针到指针到 int。你能做到吗?当然。

那么你如何处理指向某物的指针呢?指向指针的指针构成了动态分配和重新分配类型集合的支柱。它们通常被称为“动态分配的数组”,但这有点用词不当,因为不能保证所有值在内存中都是连续的。您将声明给定数量的指针int**您将声明指向数组中您不必分配相同数量的指针。

注意:不能保证指针指向的内存是连续的,尽管指针本身是连续的——请确保您理解这种区别以及“数组”保证什么以及指针不保证什么)

int** arr[5]声明五int**。然后,您可以自由地将任何地址分配给这五个指针中的每一个,只要类型是int**。例如,您将为指针分配类似于以下内容的内容:

  arr[i] = calloc (ROW, sizeof *arr[i]);  /* allocates ROW number of pointers */
Run Code Online (Sandbox Code Playgroud)

然后您可以自由分配任意数量的int地址并将该地址分配给每个指针,例如

  arr[i][j] = calloc (COL, sizeof *arr[i][j]); /* allocates COL ints */
Run Code Online (Sandbox Code Playgroud)

然后您可以循环分配值的整数:

  arr[i][j][k] = v++;
Run Code Online (Sandbox Code Playgroud)

使用类型分配的简短示例int** arr[5]可能类似于:

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

#define ROW 3
#define COL ROW
#define MAX 5

int main (void) {

    int **arr[MAX] = { NULL },  /* 5 pointer-to-pointer-to-int */
        i, j, k, v = 0;

    for (i = 0; i < MAX; i++) { /* allocate ROW pointers to each */
        if ((arr[i] = calloc (ROW, sizeof *arr[i])) == NULL) {
            perror ("calloc - pointers");
            return 1;
        }
        for (j = 0; j < ROW; j++) { /* allocate COL ints each pointer */
            if ((arr[i][j] = calloc (COL, sizeof *arr[i][j])) == NULL) {
                perror ("calloc - integers");
                return 1;
            }
            for (k = 0; k < COL; k++)   /* assign values to ints */
                arr[i][j][k] = v++;
        }
    }

    for (i = 0; i < MAX; i++) { /* output each pointer-to-pointer to int */
        printf ("pointer-to-pointer-to-int: %d\n\n", i);
        for (j = 0; j < ROW; j++) {     /* for each allocated pointer */
            for (k = 0; k < COL; k++)   /* output COL ints */
                printf ("  %4d", arr[i][j][k]);
            free (arr[i][j]);   /* free the ints */
            putchar ('\n');
        }
        free (arr[i]);      /* free the pointer */
        putchar ('\n');
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您已经分配了五个模拟二维数组,并将每个数组的指针分配给您的数组int **arr[5],输出将是:

使用/输出示例

$ ./bin/array_ptr2ptr2int
pointer-to-pointer-to-int: 0

     0     1     2
     3     4     5
     6     7     8

pointer-to-pointer-to-int: 1

     9    10    11
    12    13    14
    15    16    17

pointer-to-pointer-to-int: 2

    18    19    20
    21    22    23
    24    25    26

pointer-to-pointer-to-int: 3

    27    28    29
    30    31    32
    33    34    35

pointer-to-pointer-to-int: 4

    36    37    38
    39    40    41
    42    43    44
Run Code Online (Sandbox Code Playgroud)

希望这有助于区分指针数组和指针到指针的数组之间的区别,并展示如何声明和使用它们。如果您还有任何疑问,请随时询问。


pm1*_*100 3

指向整数的指针数组;

int x = 1;
int y = 42;
int z = 12;

int * array[3];

array[0] = &x;
array[1] = &y;
array[2] = &z;
Run Code Online (Sandbox Code Playgroud)

替代语法

int * array[] = {&x,&y,&z};
Run Code Online (Sandbox Code Playgroud)

保持简单。从那里向上工作