だから私のプログラムには構造体があります:
typedef struct Point {
double x, y;
char *label;
} point;
次に、ファイルからいくつかの情報を読み取り、配列内のさまざまなポイント構造体にさまざまなラベルを割り当てます。問題は、x、y の値が適切に割り当てられているにもかかわらず、メモリ内の各構造体のラベルが同じであることです。
point set[3];
FILE *file = fopen(argv[1], "r");
int count = 0;
char *pch;
char line[128];
if(file != NULL){
while(fgets (line, sizeof line, file) != NULL){
pch = strtok(line, " ");
int i = 0;
while(pch != NULL){
if(i==0){set[count].label = pch;}
if(i==1){set[count].x = atof(pch);}
if(i==2){set[count].y = atof(pch);}
pch = strtok(NULL, " ");
i++;
}
count++;
}
fclose(file);
}
//these values should not be the same, though they are
printf("%s\n", set[0].label);
printf("%s\n", set[1].label);
構造体を同じままにして、値を適切に割り当てることができる回避策はありますか?