|
|
|
Je suppose que tu veux stocker un tableau de chaine de caractère. Le problème c'est qu'il faut a priori connaitre le nombre de chaîne que tu vas stocker.
Je rappelle que malloc renvoie l'adresse au début d'une plage qu'il reserve et dont la taille est passé en paramètre. De manière général on écrit pour un tableau de n cases : plop * p = (plop *)malloc(n*sizeof(plop)); Il suffit de changer dans ton cas plop par char * : char ** p = (char **)malloc(n*sizeof(char *)); Pour realloc c'est le même principe. Pour plus d'infos je t'invite à taper dans une console : man malloc man calloc man realloc De manière générale faire un realloc est une mauvise idée, car il vaut mieux allouer directement la place requise (quand elle est connue :p). Bonne chance |
j pense ke là c 1 tableau double dimension donc il faudrais plutôt faire(j'ai supposais que les deux dim sont égal)
char ** p = (char **)malloc(n*n*sizeof(char *)); |
Non si c'est un tableau m * n :
unsigned i,n=5,m=7; char **tab_2d = (char *)malloc(m*sizeof(char *)); for(i=0;i<m;++i) tab_2d[i] = (char *)malloc(n*sizeof(char)); //... for(i=0;i<m;++i) free(tab_2d[i]); free(tab_2d); Bonne chance |
| 19/02 15h16 | [C] malloc et sa librairie "standard" | Programmation | 03/07 22h20 | 8 |
| 08/06 21h51 | [C] Listes chaînées + malloc | Programmation | 12/06 08h08 | 6 |
| 30/08 08h23 | [C] malloc | Programmation | 02/09 20h19 | 20 |
| 24/10 14h29 | comment malloc alloue la memoire ? | Programmation | 24/10 15h03 | 1 |