Search code examples
cstructure

How to create multiple members in a single array


i will ask user for number of matrixes then randomize a 5x5 matrix for each one. Can i do this in a single struct. If so then how should i allocate memory for it, i try to do it this way but it didn't work.

struct Temp
{
    int *mat[5][5];

}test;

int main(void)
{
    

    int x;

    printf("Enter the number of matrixes : ");
    scanf("%d", &x);

    test.mat= malloc( x* sizeof *test.mat);
    if (test.mat== NULL) {
        fprintf(stderr, "Malloc failed.\n");
        exit(1);
    }

Solution

  • int *mat[5][5];
    

    is a 5x5 array of int*.

    To create a pointer to 5x5 array of int, it should be

    int (*mat)[5][5];