首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言 构造体

2012-08-14 
c语言 结构体[codeC/C++][/code]#include stdio.h#define SIZE 5#define LEN 40struct birth{int year

c语言 结构体
[code=C/C++][/code]
#include <stdio.h>
#define SIZE 5
#define LEN 40
struct birth
{
int year;
int month;
};
struct student
{
int num;
char name[LEN];
int age;
char sex[LEN];
struct birth stu;
};

struct student getinfo(struct student *,int);
struct student showinfo(struct student);
int main(void)
{
int n=0;
int i;
struct student st[SIZE];
while(n<SIZE)
{
st[n]=getinfo(st,n);
n++;
}
return 0;

}


struct student getinfo(struct student *temp,int n)
{
int i;
printf("NO is %d\n",n+1);
temp[n].num=n+1;
printf("please input the name:\n");
scanf("%s",temp[n].name);
printf("please input the age:\n");
scanf("%d",&temp[n].age);
printf("please enter sex(0 is boy, 1 is girl)");
scanf("%d",&i);
temp[n].sex=(i==0)?"boy":"girl";
printf("please input the birth:\n");
scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);
}


程序运行错误:student.c: In function ‘getinfo’:
  student.c:46: error: incompatible types when assigning to type ‘char[40]’ from type ‘char * const’
请教需要怎么修改


[解决办法]
说的很清晰啊,类型不对,无法赋值。
不过你发帖子,排下版好吧,看起来好累,最好是这样:

C/C++ code
#include <stdio.h>#define SIZE 5#define LEN 40struct birth{int year;int month;};struct student{int num;char name[LEN];int age;char sex[LEN];struct birth stu;};struct student getinfo(struct student *,int);struct student showinfo(struct student);int main(void){int n=0;int i;struct student st[SIZE];while(n<SIZE){st[n]=getinfo(st,n);n++;}return 0;}struct student getinfo(struct student *temp,int n){int i;printf("NO is %d\n",n+1);temp[n].num=n+1;printf("please input the name:\n");scanf("%s",temp[n].name);printf("please input the age:\n");scanf("%d",&temp[n].age);printf("please enter sex(0 is boy, 1 is girl)");scanf("%d",&i);temp[n].sex=(i==0)?"boy":"girl";printf("please input the birth:\n");scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);}
[解决办法]
temp[n].sex=(i==0)?"boy":"girl";

这条语句犯下了错误:
不能够直接将字串赋值:
char str[20];
str="llo";这样是错误的,
需要使用strcpy函数。

所以你的程序报错。这是关于c的内存方面使用注意的东西,尤其需要小心
[解决办法]
参考:
C/C++ code
#include <stdio.h>#include <string.h>#define SIZE 5#define LEN 40struct birth{    int year;    int month;};struct student{    int num;    char name[LEN];    int age;    char sex[LEN];    struct birth stu;};struct student getinfo(struct student *,int);struct student showinfo(struct student);int main(void){    int n=0;    int i;    struct student st[SIZE];    while(n<SIZE)    {        st[n]=getinfo(st,n);        n++;    }    // 打印数组中的数据    for(int i = 0; i < SIZE; ++i)    {        printf("%d\t%s\t%d\t%s\t%d-%d\n", st[i].num, st[i].name, st[i].age, st[i].sex, st[i].stu.year, st[i].stu.month);    }    return 0;}struct student getinfo(struct student *temp,int n){    int i;    printf("NO is %d\n",n+1);    temp[n].num=n+1;    printf("please input the name:\n");    scanf("%s",temp[n].name);    printf("please input the age:\n");    scanf("%d",&temp[n].age);    printf("please enter sex(0 is boy, 1 is girl)");    scanf("%d",&i);    //temp[n].sex=(i==0)?"boy":"girl";   // 改为下面一句    strcpy(temp[n].sex, (i==0)?"boy":"girl");      printf("please input the birth:\n");    scanf("%d%d",&temp[n].stu.year,&temp[n].stu.month);    return temp[n];  // 增加这一句} 


[解决办法]
都是基础的知识。。。字符串赋值都是strcpy。。

热点排行