-4

行数ではなく列数の値がわかっている場合、mallocを使用してCで配列を割り当てることができるかどうかを尋ねたかった.

int Array[runtime value][N];
4

2 に答える 2

1

はい。動的に割り当てることができます。

// Allocate the columns
int** two_dimensional_array = malloc(COLUMNS * sizeof(int*));

// Find the number of rows on runtime
// however you please.

// Allocate the rest of the 2D array
int i;
for (i = 0; i < COLUMNS; ++i) {
    two_dimensional_array[i] = malloc(sizeof(int) * ROWS);
}

または、可変サイズ (C99) でスタック上に 1 つ持つことができます。

int n;
scanf("%d", &n);

int arr[n][COLUMNS];
于 2013-07-25T06:05:47.000 に答える