-1

I am practicing a 2 dimensional array.

Although I want to assign a set of numbers to a row by row, which of 2 dimensional array has already been made, my code can not be compiled.

How can I achieve my purpose, and what is my mistake?

Thank you for your reading my question.

#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;   

}
TADASUKE
  • 95
  • 8
  • Thank you so much, everyone! I have could solve my problem with one way. I will try other ways also. – TADASUKE Aug 29 '20 at 13:04

3 Answers3

1

You do everything fine, up until int animation[0]= ...

int animation[0]= ... declares a new array variable of size zero elements. Your compiler will complain about this because there is already an int **animation variable.

Because you allocate the array dynamically using malloc, you cannot initialize it with static initializers. It means you have to initialize them "by hand", for example

animation[0][0]= 0;
animation[0][1]= 40;
/// etc.
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Hello! My referencing site's sample code has lines like your showing ones. I misunderstood, in addition to writing any number into a space by space, I can assign them into a line by one statement. Thank you so much! – TADASUKE Aug 29 '20 at 10:10
1

You can use an array with the initial values and the memcpy function, like this:

#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]);
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
1

Your code has three main issues.

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};

is a mix of attempting to declare new arrays and initialize them instead of to initialize the memory animation points to properly. I recommend you to read a chapter of good C book again about how to declare and initialize arrays in C.

Use instead:

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

%s is only to print character strings. Not int arrays! Please learn more about strings.

Use

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);

You attempt to free memory several times due to the faulty y = +1. Change it to y++.


Full corrected code:

#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;   
}

Execution/Output (Online Example):

./a.out
40 48 118 48 41 0 0 
40 48 118 48 41 0 0 
40 48 118 48 41 0 0 

Please learn more about C from a good starting book. The issues you made show that you currently ain't got sufficient knowledge.

A list of good books you can find here:

  • Thank you so much for your providing quick answer! I did not think I had tried "attempting to declare new arrays and initialize them." I will check your advice by a line by line and any resource contianed by your providing page. – TADASUKE Aug 29 '20 at 10:03
  • 1
    @CaptainCookie Some books are available as free PDF copy. F.e. [Modern C](https://modernc.gforge.inria.fr/) by community member Jens Gustedt. – RobertS supports Monica Cellio Aug 29 '20 at 14:06
  • I got it! Before ordering at a web site, I will check weather it is available on the official site or not as you advised. I had a exeperience to have got some reference book at author's official site. Thank you for your additional support! Now I have re-written my code. – TADASUKE Aug 29 '20 at 14:34