79214137

Date: 2024-11-22 08:17:05
Score: 1
Natty:
Report link

I got something to work by just copying the values of a static array and placing them into the dynamic one like this,

void f(int r, int c, int **grid);

int main() {
    int r = 3, c = 5;
    int values[][5] = {
        {0, 1, 2, 3, 4},
        {1, 2, 4, 2, 1},
        {3, 3, 3, 3, 3}
    };
    int **test = malloc(r*sizeof(int*));
    for (int i = 0; i < r; i++) {
        test[i] = malloc(c*sizeof(int));
        for (int j = 0; j < c; j++) {test[i][j] = values[i][j];}
    }
    f(r, c, test);
    return 0;
}

However I would need to specify the number of columns of the static array every time I do testing. Is there a shorter way to do this using Compound Literals and without creating a values variable? I am using GCC 6.3.0 compiler for C.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Therd Dollison