kotasha
kotasha

Reputation: 45

Dynamically Allocating Multidimensional Arrays

I've a problem of execution when i trey to run this program, it's simply to allocate dynamically a multidimensional array with chosen values and just zero it. it compiles correctly but it doesn't execute.

    #include<stdio.h>
    #include<stdlib.h>

    int main(void) {
        int **tab;
        int ligne;
        int col;
        printf("saisir le nbre de lignes volous\n");
        scanf("%d", &ligne);
        printf("saisir le nbre de colonnes volous\n");
        scanf("%d", &col);
        tab = (int**)malloc(ligne*sizeof(int*));
        int i ,j;
        for (i=0 ; i < ligne; i++) {
            *(tab+i) = (int*)malloc(col*sizeof(int));
        }
        for (i = 0; i < ligne; i++) {
            for (j = 0; j < col; j++) {
                **(tab + i+ j) = 0;
            }
        }
        for (i = 0; i < ligne; i++) {
            for (j = 0; j < col; j++) {
                printf("%d\t", **(tab + i +j));
            }
            printf("\n");
        }
        free(tab);
        return 0;
    }

thank you.

Upvotes: 0

Views: 88

Answers (4)

Luis Colorado
Luis Colorado

Reputation: 12708

As you have allocated your arrays, (the one dimensional parts) your array can be addressed as table[i][j], and never as you do in

for (i = 0; i < ligne; i++) {
    for (j = 0; j < col; j++) {
        **(tab + i+ j) = 0;  /* <--- this is an error */
    }
}

as you see tab + i + j is a pointer to which you offset i (the ligne number) plus j (the col number) and both aren't actually the same size (columns are one cell size and rows are one line size) You'd better to write tab[i][j] as tab[i] is a pointer (allocated with malloc(3)) that points to a single dimensional array (and the different pointers tab[0], tab[1],... tab[n] don't have to be correlated between them, as they come from distinct malloc() calls) If you don't like the brackets notation, then you should write the equivalent

*(*(mat + i) + j)  /* equivalent to mat[i][j] */

and never the notation you use in your code.

**(tab + i + j)  /* equivalent to *mat[i + j] */

Upvotes: 0

AndersK
AndersK

Reputation: 36092

Here, I did some changes and added some comments to the changes

#include<stdio.h>
#include<stdlib.h>

int main(void) {
    int **tab = NULL;
    int ligne = 0;
    int col = 0;
    char buffer[128] = {0};

    printf("saisir le nbre de lignes volous\n");

    // to avoid leaving \n in buffer after you enter the first value
    // you should also check also return value of fgets 
    // and quit program if it returns NULL
    // in general it is good practice to check return values
    // of all run-time functions.

    if (fgets(buffer,sizeof(buffer),stdin)==NULL) {
       return 1;
    }
    ligne = atoi(buffer);

    printf("saisir le nbre de colonnes volous\n");
    if (fgets(buffer,sizeof(buffer),stdin) == NULL) {
       return 1;
    }
    col = atoi(buffer);

    tab = malloc(ligne*sizeof(int*)); // do not cast malloc
    int i ,j;

    // use tab[i] and tab[i][j] syntax, it is easier to read

    for (i=0 ; i < ligne; i++) {
        tab[i] = malloc(col*sizeof(int)); 
    }
    for (i = 0; i < ligne; i++) {
        for (j = 0; j < col; j++) {
            tab[i][j] = 0;
        }
    }
    for (i = 0; i < ligne; i++) {
        for (j = 0; j < col; j++) {
            printf("%d\t", tab[i][j]);
        }
        printf("\n");
    }

    // before you free tab, you need to free all lines
    for (i=0 ; i < ligne; i++) {
        free(tab[i]);
    }

    free(tab);
    return 0;
}

Upvotes: 0

imtorres
imtorres

Reputation: 21

int main(void) {
    int ligne;
    int col;
    printf("saisir le nbre de lignes volous\n");
    scanf("%d", &ligne);
    printf("saisir le nbre de colonnes volous\n");
    scanf("%d", &col);

    int tableSize = ligne * (col*sizeof(int));
    int * table = (int*) malloc(tableSize);

    int i,j;
    for (i=0 ; i < ligne; i++) {
          for (j = 0; j < col; j++) {
              *(table + i+ j) = 0;
          }

    }

    for (i = 0; i < ligne; i++) {
        for (j = 0; j < col; j++) {
            printf("%d\t", *(table + i +j));
        }
        printf("\n");
    }
    free(table);
    return 0;
}

Upvotes: 0

LPs
LPs

Reputation: 16243

Use simple [row][col] access to your double pointer. It is more readable and you can avoid errors, as you coded.

#include<stdio.h>
#include<stdlib.h>

int main(void) {
    int **tab;
    int ligne;
    int col;
    printf("saisir le nbre de lignes volous\n");
    scanf("%d", &ligne);
    printf("saisir le nbre de colonnes volous\n");
    scanf("%d", &col);

    tab = malloc(ligne*sizeof(int*));

    if (tab != NULL)
    {
        int i ,j;
        for (i=0 ; i < ligne; i++)
        {
            tab[i] = malloc(col*sizeof(int));
            if (tab[i] == NULL)
            {
                fprintf(stderr, "Malloc failed\n");
                return 1;
            }

        }
        int k=0;
        for (i = 0; i < ligne; i++) {
            for (j = 0; j < col; j++) {
                tab[i][j] = k++;
            }
        }
        for (i = 0; i < ligne; i++) {
            for (j = 0; j < col; j++) {
                printf("%d\t", tab[i][j]);
            }
            free(tab[i]);
            printf("\n");
        }
    }
    free(tab);
    return 0;
}

Upvotes: 1

Related Questions