I am trying to define a two dimensional array by initially defining elements for 5 x 2 matrix and then I am defining again the elements for 6th row. But when I try to print the elements of this matrix I am getting 0 and 5 for the last value. Tried same by defining elements again for 4th or 6th row but then it is working fine.
我试图通过最初定义5 x 2矩阵的元素来定义二维数组,然后我再次定义第6行的元素。但是当我尝试打印这个矩阵的元素时,我得到0和5代表最后一个值。通过为第4行或第6行再次定义元素来尝试相同但是它工作正常。
#include<math.h>
#include<stdio.h>
main()
{
int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
int i, j;
arr[5][0]=61; arr[5][1]=62;
for (i=0;i<=5;i++)
{
for (j=0;j<=1;j++)
{
printf ("%d \n", arr[i][j]);
}
}
}
1 个解决方案
#1
Your initialised array is given exactly enough memory to hold the specified data values. So
初始化的数组具有足够的内存来保存指定的数据值。所以
int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
creates the array as int arr[5][2]
and then the line
创建数组为int arr [5] [2]然后是行
arr[5][0]=61; arr[5][1]=62;
exceeds the array bounds. The maximum index is [4][1]
because array indexing is 0
based. If you want to add another element you should specify
超出数组范围。最大索引是[4] [1],因为数组索引是基于0的。如果要添加另一个元素,则应指定
int arr[6][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
and then this line will work.
然后这一行将起作用。
arr[5][0]=61; arr[5][1]=62;
An alternative would be to use malloc()
to allocate memory for the array, and then if you want to add another row you can use realloc()
, and this shows how to make a flexible array.
另一种方法是使用malloc()为数组分配内存,然后如果要添加另一行,可以使用realloc(),这将显示如何创建一个灵活的数组。
#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 2
#define ROWS 5
typedef int rowtype[COLUMNS];
int main() {
int i, j;
rowtype *array = malloc(ROWS * sizeof(rowtype));
if (array == NULL)
return -1;
for (j=0; j<ROWS; j++)
for (i=0; i<COLUMNS; i++)
array[j][i] = (j+1)*10 + (i+1);
for (j=0; j<ROWS; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
printf("Add another row\n");
array = realloc(array, (ROWS+1) * sizeof(rowtype));
if (array == NULL)
return -1;
array[ROWS][0] = 61;
array[ROWS][1] = 62;
for (j=0; j<ROWS+1; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
free(array);
return 0;
}
Program output:
11 12
21 22
31 32
41 42
51 52
Add another row
11 12
21 22
31 32
41 42
51 52
61 62
#1
Your initialised array is given exactly enough memory to hold the specified data values. So
初始化的数组具有足够的内存来保存指定的数据值。所以
int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
creates the array as int arr[5][2]
and then the line
创建数组为int arr [5] [2]然后是行
arr[5][0]=61; arr[5][1]=62;
exceeds the array bounds. The maximum index is [4][1]
because array indexing is 0
based. If you want to add another element you should specify
超出数组范围。最大索引是[4] [1],因为数组索引是基于0的。如果要添加另一个元素,则应指定
int arr[6][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
and then this line will work.
然后这一行将起作用。
arr[5][0]=61; arr[5][1]=62;
An alternative would be to use malloc()
to allocate memory for the array, and then if you want to add another row you can use realloc()
, and this shows how to make a flexible array.
另一种方法是使用malloc()为数组分配内存,然后如果要添加另一行,可以使用realloc(),这将显示如何创建一个灵活的数组。
#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 2
#define ROWS 5
typedef int rowtype[COLUMNS];
int main() {
int i, j;
rowtype *array = malloc(ROWS * sizeof(rowtype));
if (array == NULL)
return -1;
for (j=0; j<ROWS; j++)
for (i=0; i<COLUMNS; i++)
array[j][i] = (j+1)*10 + (i+1);
for (j=0; j<ROWS; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
printf("Add another row\n");
array = realloc(array, (ROWS+1) * sizeof(rowtype));
if (array == NULL)
return -1;
array[ROWS][0] = 61;
array[ROWS][1] = 62;
for (j=0; j<ROWS+1; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
free(array);
return 0;
}
Program output:
11 12
21 22
31 32
41 42
51 52
Add another row
11 12
21 22
31 32
41 42
51 52
61 62