Allocation de memoire pour structure.

Résolu/Fermé
rocksider Messages postés 106 Date d'inscription mardi 21 octobre 2008 Statut Membre Dernière intervention 16 mars 2012 - 2 déc. 2008 à 22:19
rocksider Messages postés 106 Date d'inscription mardi 21 octobre 2008 Statut Membre Dernière intervention 16 mars 2012 - 15 déc. 2008 à 01:41
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++;
}
}

4 réponses

mype Messages postés 2435 Date d'inscription jeudi 1 novembre 2007 Statut Membre Dernière intervention 16 août 2010 436
2 déc. 2008 à 22:39
ton typedef il est pas correct
essaye comme ça
typedef struct coord
{ 
int x,y; 
}coord;
0
rocksider Messages postés 106 Date d'inscription mardi 21 octobre 2008 Statut Membre Dernière intervention 16 mars 2012 9
2 déc. 2008 à 22:50
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
0
mype Messages postés 2435 Date d'inscription jeudi 1 novembre 2007 Statut Membre Dernière intervention 16 août 2010 436
3 déc. 2008 à 01:56
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 matrice
for(j=0;j<longeur;j++) 
{ 
laby[i][j].x=j; 
laby[i][j].y=i;  
}
0
rocksider Messages postés 106 Date d'inscription mardi 21 octobre 2008 Statut Membre Dernière intervention 16 mars 2012 9
15 déc. 2008 à 01:41
merci ^^
0