我正在练习一个二维数组。
虽然我想一行一行地给一组数字赋值,但二维数组中的哪一个已经制作好了,我的代码无法编译。
我怎样才能达到我的目的,我的错误是什么?
谢谢你阅读我的问题。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int **animation;
int rows = 3;
int cols = 7;
int y, x;
int frame;
animation = (int **) malloc(rows * sizeof(int *));
if (animation == NULL) {
puts("array animation rows: allocation failure!");
return 1;
}
for (y = 0; y < rows; y = y + 1) {
animation[y] = (int *) malloc(cols * sizeof(int));
if (animation[y] == NULL) {
printf("array animation[%d]: allocation failure!", y);
return 1;
}
}
int animation[0] = {0, 40, 48, 118, 48, 41, 0};
int animation[1] = {40, 48, 118, 48, 41, 0, 0};
int animation[2] = {40, 48, 118, 48, 41, 0, 0};
for (frame = 0; frame != 3; frame = frame + 1) {
printf("%sn", animation[frame]);
}
for (y = 0; y < rows; y = +1) {
free(animation[y]);
}
free(animation);
return 0;
}
您的代码有三个主要问题。
1
int animation[0] = {0, 40, 48, 118, 48, 41, 0};
int animation[1] = {40, 48, 118, 48, 41, 0, 0};
int animation[2] = {40, 48, 118, 48, 41, 0, 0};
是尝试声明新数组并对其进行初始化的混合,而不是正确初始化animation
指向的内存。我建议您再次阅读优秀的C书中关于如何在C.中声明和初始化数组的一章
改为使用:
animation[0][0] = 40;
animation[0][1] = 48;
animation[0][2] = 118;
animation[0][3] = 48;
animation[0][4] = 41;
animation[0][5] = 0;
animation[0][6] = 0;
for (int i = 1; i < rows; i++)
memcpy(animation[i], animation[i-1], sizeof(int) * cols);
2
printf("%sn", animation[frame]);
%s
仅用于打印字符串。不是int
阵列!请详细了解字符串。
使用
for (frame = 0; frame != 3; frame = frame + 1) {
for (int i = 0; i < cols; i++)
printf("%d ", animation[frame][i]);
printf("n");
}
3
for (y = 0; y < rows; y = +1) {
free(animation[y]);
}
free(animation);
由于y = +1
出现故障,您多次尝试释放内存。将其更改为y++
。
完全校正代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int **animation;
int rows = 3;
int cols = 7;
int y;
int frame;
animation = (int **) malloc(rows * sizeof(int *));
if (animation == NULL) {
fputs("array animation rows: allocation failure!",stderr);
return 1;
}
for (y = 0; y < rows; y = y + 1) {
animation[y] = (int *) malloc(cols * sizeof(int));
if (animation[y] == NULL) {
fprintf(stderr,"array animation[%d]: allocation failure!", y);
return 1;
}
}
animation[0][0] = 40;
animation[0][1] = 48;
animation[0][2] = 118;
animation[0][3] = 48;
animation[0][4] = 41;
animation[0][5] = 0;
animation[0][6] = 0;
for (int i = 1; i < rows; i++)
memcpy(animation[i], animation[i-1], sizeof(int) * cols);
for (frame = 0; frame < rows; frame = frame + 1) {
for (int i = 0; i < cols; i++)
printf("%d ", animation[frame][i]);
printf("n");
}
for (y = 0; y < rows; y++) {
free(animation[y]);
}
free(animation);
return 0;
}
执行/输出(在线示例(:
./a.out
40 48 118 48 41 0 0
40 48 118 48 41 0 0
40 48 118 48 41 0 0
请从一本好的入门书中了解更多关于C的信息。你提出的问题表明你目前还没有足够的知识。
你可以在这里找到一份好书清单:
- 最终C书指南和列表
您做得很好,直到int animation[0]= ...
CCD_ 7声明一个大小为零元素的新数组变量。您的编译器会抱怨这一点,因为已经有一个int **animation
变量了。
因为使用malloc
动态分配数组,所以无法使用静态初始化器对其进行初始化这意味着你必须初始化它们"手工";,例如
animation[0][0]= 0;
animation[0][1]= 40;
/// etc.
您可以使用带有初始值的数组和memcpy函数,如下所示:
#define ROWS 3
#define COLS 7
int initialValues[ROWS][COLS] = {
{0, 40, 48, 118, 48, 41, 0},
{40, 48, 118, 48, 41, 0, 0},
{40, 48, 118, 48, 41, 0, 0}};
...
for (y = 0; y < ROWS; y++) {
memcpy(animation[y], initialValues[y], sizeof initialValues[y]);
}