0

我正在尝试使用结构“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);
}

在我的代码中,我有三个单独的数组,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");
4

1 回答 1

0

使用指向指针的指针。

sommet** somtab = malloc(nbnoeud*sizeof(sommet));

利用

    somtab[a]->x = xtab[a];
    somtab[a]->y = ytab[a];
    strcpy(somtab[a]->nom, nomtab[a]);
于 2013-04-15T12:49:54.047 回答