為什么名字打不出來?
#include?<stdio.h> struct?nam { char?name[20]; int?old; float?height; struct?nam?*?next; }; int?main() { struct?nam?a,b,?*head; a.name="李"; a.old=16; a.height=70.2; b.name="林"; b.old=21; b.height=65.3; head=&a; a.next=&b; b.next=NULL; struct?nam?*w; w=head; while?(w!=NULL){ printf("姓名:%s\n年齡:%d\n身高:%f\n",w->name,w->old,w->height); w=w->next; } return?0; }
錯誤顯示為?[Error] incompatible types in assignment of 'const char [3]' to 'char [20]'
2016-05-28
#include <stdio.h>
?
struct nam
{
? ? char *name[20]; ?//用指針
? ? int old;
? ? float height;
? ? struct nam * next;
};
int main()
{
? ? struct nam a,b, *head;
? ? a.name[0]="李"; ?//第一個地址
? ? a.old=16;
? ? a.height=70.2;
? ? b.name[0]="林";
? ? b.old=21;
? ? b.height=65.3;
? ? head=&a;
? ? a.next=&b;
? ? b.next=NULL;
? ? struct nam *w;
? ? w=head;
? ? while (w!=NULL){
? ? ? ? printf("姓名:%s\n年齡:%d\n身高:%f\n",w->name[0],w->old,w->height); //把地址取出來
? ? ? ? w=w->next;
? ? }
? ? return 0;
}
//我就想到這個辦法能打印名字出來
2019-04-27
因為name實際上是一個指向字符串的指針,但是不能改變指向。用賦值運算符將另一個字符串賦值給她實際上就是改變了name的指向,這顯然是錯誤的。可以用字符串操作函數strcpy來將另一個字符串copy給name。
2016-05-30
#include <stdio.h>
struct new{
? char name[20];
? int age;
? float weight;
? float hight;
? struct new *next;
};
int main()
{
? struct new a={"lijianhui",27,60,173};
? struct new b={"dengchao",25,40,158};
? struct new c={"chenwei",27,70,173};
? struct new d={"dengjie",27,50,158};
? struct new *head=&a;
? a.next=&b;
? b.next=&c;
? c.next=&d;
? d.next=NULL;
? struct new *p;
? p=head;
? while(p!=NULL)
? {
???? printf("%s,%d,%.f,%.f\n",p->name,p->age,p->weight,p->hight);
???? p=p->next;
? }
return 0;
}
2016-05-27
數據不兼容