typedef struct Data* DATAS;
struct Data {
char *name;
char *city;
DATAS next;
};
typedef struct Data DATA;
int main(void){
DATAS tmp;
tmp=(DATAS) malloc(sizeof(DATA));
printf("please enter name:\n");
scanf("%s",&tmp->name);
printf("%s\n",&tmp->name);
printf("please enter city:\n");
scanf("%s",&tmp->city);
printf("%s\n",&tmp->name);
printf("%s\n",&tmp->city);
return 0;
}
這是一個家庭作業的一部分。或者說是這個概念。我需要使用'typedef struct Data * DATAS'這是拋棄我。當我運行這個時,我用城市的一部分覆蓋了名字,所以我得到了這個結果。C結構與指針認爲我沒有正確分配內存
please enter name:
name
name
please enter city:
city
namecity
city
任何幫助將是偉大的。謝謝。我已經使用
tmp=(DATAS) malloc(sizeof(DATA));
tmp=(DATA) malloc(sizeof(DATA));
這太好了。沒有比一般的指針更令人沮喪的了。正當我想我已經把它放下時,我感到困惑。我正在使用'&tmp-> name',因爲你指出'tmp-> name'崩潰了。我認爲'tmp =(DATAS)malloc(sizeof(DATA));'會爲包括'name'和'city'在內的整個結構分配內存。 – ScottEdge 2012-08-05 03:50:54
我想你會發現[comp.lang.c FAQ](http://www.c-faq.com/)是一個有價值的資源。 – 2012-08-05 03:53:04