卡布奇诺540 2021-10-12 14:50 采纳率: 0%
浏览 35

p指针为什么不能给element的name,id,这些初始化

img


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student
{
char name[20];
char id[12];
char sex[5];
int age;
};
typedef struct student DataType;

struct SeqList
{
int max;
int n;
DataType *element;
};
typedef struct SeqList *PSeqList;

//创建空顺序表
PSeqList create(int m)
{
PSeqList plist = (PSeqList)malloc(sizeof(struct SeqList));
if(plist!=NULL)
{
plist->element = (DataType *)malloc(sizeof(struct student)*m);
if(plist->element)
{
plist->max=m;
plist->n=0;
return plist;
}
else free(plist);
}
printf("Out of space\n");
return NULL;
}

int main()
{
PSeqList p;
p=create(10);
printf("姓名 学号 性别 年龄\n");
p->element[0].name="张三",p->element[0].id="12020242562",p->element[0].sex="男",p->element[0].age=20;
printf("%s %s %s %d",p->element[0].name,p->element[0].id,p->element[0].sex,p->element[0].age);
}

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2021-10-12 14:53
    关注

    p->element[0].name="张三",p->element[0].id="12020242562",p->element[0].sex="男"这样不行,改为:
    strcpy(p->element[0].name,"张三");
    strcpy(p->element[0].id,"12020242562");
    strcpy(p->element[0].sex,"男");

    评论

报告相同问题?

问题事件

  • 创建了问题 10月12日

悬赏问题

  • ¥30 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题