[英]Accessing values from an array of structures
I'm trying to use an array of a the structure 'sommet', as defined below. 我正在尝试使用结构'sommet'的数组,如下所示。
typedef struct sommet {
double x;
double y;
char nom[100];
struct arete2* voisin;};
sommet* somtab = (sommet *) calloc(nbnoeud, sizeof(sommet));
if(somtab = NULL){
puts("Calloc error");
exit(1);
}
In my code I have three separate arrays, xtab, ytab and nomtab, that respectively contain the x, y, and nom values for the somtab element of equivalent index (ie somtab[0].x = xtab[0], somtab[1].x = xtab[1] etc.) When come to set each of the somtab elements my programme breaks down and I never make it to "check3" 在我的代码中,我有三个独立的数组xtab,ytab和nomtab,它们分别包含等效索引的somtab元素的x,y和nom值(即somtab [0] .x = xtab [0],somtab [1 ] .x = xtab [1]等。)当设置每个somtab元素时,我的程序崩溃了,我再也没有将其设置为“ check3”
puts("check1");
for(a=0; a<nbnoeud; a++){
printf("Read item %d; \t \t \t %s - (%lf, %lf). \n", a, nomtab[a], xtab[a], ytab[a]);
}
puts("check2");
for(a=0; a<nbnoeud; a++){
somtab[a].x = xtab[a];
somtab[a].y = ytab[a];
strcpy(somtab[a].nom, nomtab[a]);
}
puts("check3");
Use a pointer to a pointer. 使用指向指针的指针。
sommet** somtab = malloc(nbnoeud*sizeof(sommet));
use 采用
somtab[a]->x = xtab[a];
somtab[a]->y = ytab[a];
strcpy(somtab[a]->nom, nomtab[a]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.