Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays C编程:制作二维数组后,如何将一组整数逐行赋值?_Arrays_C_Pointers_Multidimensional Array_Memory Management - Fatal编程技术网

Arrays C编程:制作二维数组后,如何将一组整数逐行赋值?

Arrays C编程:制作二维数组后,如何将一组整数逐行赋值?,arrays,c,pointers,multidimensional-array,memory-management,Arrays,C,Pointers,Multidimensional Array,Memory Management,我在练习二维数组 虽然我想把一组数字一行一行地分配给一个二维数组,但我的代码无法编译 我怎样才能达到我的目的,我的错误是什么 谢谢你阅读我的问题 #include <stdio.h> #include <stdlib.h> int main(void) { int **animation; int rows = 3; int cols = 7; int y, x; int frame; animation = (

我在练习二维数组

虽然我想把一组数字一行一行地分配给一个二维数组,但我的代码无法编译

我怎样才能达到我的目的,我的错误是什么

谢谢你阅读我的问题

#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("%s\n", animation[frame]);
    
    }



    for (y = 0; y < rows; y = +1) {
        free(animation[y]);
    }

    free(animation);
    
    return 0;   

}

在int动画[0]=

int动画[0]=。。。声明大小为零元素的新数组变量。您的编译器将对此进行投诉,因为已经存在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]);
}

您的代码有三个主要问题

1. 是尝试声明新数组并初始化它们而不是正确初始化内存动画点的混合。我建议您再次阅读一本优秀的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);
%s仅用于打印字符串。不是整数数组!请了解有关字符串的更多信息

使用

执行/输出:

请从一本好的入门书中了解更多关于C的知识。你提出的问题表明你目前没有足够的知识

您可以在此处找到好书列表:


非常感谢您提供的快速回答!我认为我没有尝试声明新数组并初始化它们。我将逐行检查您的建议以及您提供的页面所包含的任何资源。@CaptainCookie有些书是免费的PDF副本。社区成员Jens Gustedt的F.e.我明白了!在网站上订购之前,我会检查官方网站上是否提供该产品。我有一个经验,在作者的官方网站上找到了一些参考书。感谢您的额外支持!现在我已经重新编写了我的代码。你好!我的引用站点的示例代码有一些行与您显示的行类似。我误解了,除了把任何数字一个一个地写进一个空格里,我还可以把它们一行一行地写进一个语句里。非常感谢你!谢谢你的建议。这些台词对我来说是新的。我将尝试这些步骤!非常感谢大家!我可以用一种方法解决我的问题。我也会尝试其他方法。
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);
printf("%s\n", animation[frame]);
for (frame = 0; frame != 3; frame = frame + 1) {
    for (int i = 0; i < cols; i++)
        printf("%d ", animation[frame][i]);  

    printf("\n");  
}
for (y = 0; y < rows; y = +1) {
    free(animation[y]);
}

free(animation);
#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