Allocation de memoire pour structure.
Résolu
rocksider
Messages postés
107
Statut
Membre
-
rocksider Messages postés 107 Statut Membre -
rocksider Messages postés 107 Statut Membre -
Bonjour,
Voilà j'ai voulu créé une matrice d'une structure (comportante 2 int, et puis les initialisé ), mais le code plante dans l'initialisation voilà le bout de code:
typedef struct
{
int x,y;
}coord;
...
laby=(coord **)malloc(profond*sizeof(coord*));
for(i=0;i<profond;i++)
{
*(laby+i)=(coord *)malloc(longeur*sizeof(coord));
for(j=0;j<longeur;j++)
{
laby.x=j;
laby.y=i;
laby++;
}
}
Voilà j'ai voulu créé une matrice d'une structure (comportante 2 int, et puis les initialisé ), mais le code plante dans l'initialisation voilà le bout de code:
typedef struct
{
int x,y;
}coord;
...
laby=(coord **)malloc(profond*sizeof(coord*));
for(i=0;i<profond;i++)
{
*(laby+i)=(coord *)malloc(longeur*sizeof(coord));
for(j=0;j<longeur;j++)
{
laby.x=j;
laby.y=i;
laby++;
}
}
Configuration: Windows XP Firefox 3.0.4
4 réponses
-
ton typedef il est pas correct
essaye comme çatypedef struct coord { int x,y; }coord; -
merci j'ai changer, mais toujours la meme erreur, mais sinon c'est quoi l'erreur, j'utilise toujours les structures comme j'ai fait dans l'exemple et il marche normalement
-
ben normalement la syntaxe d'un typedef c'est typedef type nouveau_type sachant que struct n'est pas un type le type c'est struct coord
pour ton probleme dans la boucle il faut utilisé laby comme une matricefor(j=0;j<longeur;j++) { laby[i][j].x=j; laby[i][j].y=i; } -