遇到了稍微复杂一点的代码部分的问题,我已经把它剥离了,但错误仍然存在。你能粗略地看看这个并指出我的错误吗?
//before this, nbnoeud is defined and graphe is a stream that reads from a .txt file
double* xtab = (double *) calloc(nbnoeud, sizeof(double));
double* ytab = (double *) calloc(nbnoeud, sizeof(double));
char** nomtab = (char **) calloc(nbnoeud, 100*sizeof(char));
double x, y; char nom[100]; int numero=0, scancheck;
int a;
for(a=0; a<nbnoeud; a++){
scancheck = fscanf(graphe, "%d %lf %lf %[^\n]", &numero, &x, &y, nom);
if(scancheck = 4) printf("Read item %d; \t Scancheck: %d; \t %s - (%lf, %lf). \n", numero, scancheck, nom, x, y);
xtab[a] = x;
ytab[a] = y;
nomtab[a] = nom; I imagine it's something to do with compatibility of this but to my eyes it all checks out
/*int b; //this section just illustrates that only the previously accessed elements are being overwritten. See code2.png
for(b=0; b<=nbnoeud; b++){
printf("%s \n", nomtab[b]);
}*/
}
for(a=0; a<nbnoeud; a++){
printf("Read item %d; \t \t \t %s - (%lf, %lf). \n", a, nomtab[a], xtab[a], ytab[a]);
}
exit(1);
当我nomtab[0]
通过[7]
( nbnoeud = 8
,在这种情况下) 进行打印时,问题就出现了,因为所有值 ( nomtab[0]
,nomtab[1]
等) 都等于写入的最终值。奇怪的是,经过检查,只有已经访问过的元素nomtab
被覆盖。例如,在第一个循环之后,nomtab[0]= Aaa
其余的等于null
;在第二个循环之后,nomtab[0]
&nomtab[1] = Baa
和其余的相等null
(见第二张图片)。我想对此有一个愚蠢的简单解决方案,但这使得不知道更加难以忍受。
我也尝试过使用strcpy
,但它不喜欢那样。
有任何想法吗?
输出:
每次循环后检查数组内容的输出