0

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.

asked Nov 4, 2016 at 16:27
5
  • yeah actually it is :) thank you Commented Nov 4, 2016 at 16:38
  • use fgets to read from keyboard, then sscanf to readout the value or simply use atoi to convert. Commented Nov 5, 2016 at 10:56
  • **(tab+i+j) is the wrong way to access element. You could use tab[i][j]. Commented Nov 5, 2016 at 11:15
  • What do you mean with it doesn't execute? Commented Nov 7, 2016 at 7:57
  • Don't cast the result of malloc in C Commented Nov 7, 2016 at 8:09

4 Answers 4

1

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;
}
answered Nov 4, 2016 at 16:36
Sign up to request clarification or add additional context in comments.

Comments

0
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;
}
answered Nov 4, 2016 at 16:59

Comments

0

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;
}
answered Nov 5, 2016 at 11:06

Comments

0

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] */
answered Nov 7, 2016 at 8:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.