qq_隐匿_03229380
2016-05-26 23:34
#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]'
#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;
}
//我就想到这个办法能打印名字出来
因为name实际上是一个指向字符串的指针,但是不能改变指向。用赋值运算符将另一个字符串赋值给她实际上就是改变了name的指向,这显然是错误的。可以用字符串操作函数strcpy来将另一个字符串copy给name。
#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;
}
数据不兼容
Linux C语言结构体
118294 学习 · 162 问题
相似问题