我有一個結構,並在一個結構我有一個字符指針,但是,我創建此結構的不同實例,但是當我更改指針在一個結構中另一個也在改變。如何讓結構指向不同的字符串?
#include <stdio.h>
#include <stdlib.h>
typedef struct human{
int age;
char name[100];
} Human;
int main(){
FILE *s = fopen("h.txt","r");
if(s==NULL){
printf("file not available");
}
for(int i=0 ;i<5;i++){
Human h;
fscanf(s,"%d",&h.age);
fscanf(s,"%s",h.name);
insertintolinkedlist(h);
// this method is going to insert the human into the linked list
}
return 0;
}
發生了什麼事情,鏈接列表中的所有人都有不同的年齡,但同名!
h.name沒有分配給它的內存。根據提供的示例代碼,我期望第二個fscanf會崩潰。 – rrrzx
@rrrzx爲什麼會崩潰? –