-tthappy 2023-04-29 17:33 采纳率: 55.6%
浏览 14
已结题

线性表顺序存储无法给字符数组赋值

关于线性表顺序存储结构想给商品命名,
为什么程序运行不了?怎么修改才能正确呢?
求指教,谢谢!

img

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h> 
#define OK 1
#define ERROW 0
#define MAXSIZE 1000
long int key=123456;//管理员初始密码 
typedef struct goods  //商品信息 
{char name[20];    //商品名 
int price;             //商品价格 
int Qty;           //商品剩余数量  新增功能 
}goods;

typedef struct sqlist
{goods *elem;    //利用线性表顺序存储结构,数组空间基地址 
int lenth;              //上货商品种类数目 
}sqlist;

int Initlist(sqlist &L)     //商品初始化,即最初始的商品有题目要求的基本十个物品 
{L.elem=(goods*)malloc(sizeof(goods)*MAXSIZE);  
L.lenth=10;
L.elem[0].name ="Pure Water ";
L.elem[0].price=3,L.elem[0].Qty=10;
return OK;
 } 

int main()
{sqlist L;
Initlist (L);
printf("%s",L.elem[0].name);
return 0;
}


  • 写回答

1条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-04-29 17:50
    关注
    • 使用 strcpy 吧
    • 修改如下:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    #include <stdlib.h> 
    #define OK 1
    #define ERROW 0
    #define MAXSIZE 1000
    long int key=123456;//管理员初始密码 
    typedef struct goods  //商品信息 
    {
        char name[20];    //商品名 
        int price;        //商品价格 
        int Qty;          //商品剩余数量  新增功能 
    }goods;
     
    typedef struct sqlist
    {
        goods *elem;    //利用线性表顺序存储结构,数组空间基地址 
        int lenth;      //上货商品种类数目 
    }sqlist;
     
    int Initlist(sqlist &L)     //商品初始化,即最初始的商品有题目要求的基本十个物品 
    {
        L.elem=(goods*)malloc(sizeof(goods)*MAXSIZE);  
        L.lenth=10;
        strcpy(L.elem[0].name, "Pure Water "); // 
        L.elem[0].price=3,L.elem[0].Qty=10;
        return OK;
    } 
     
    int main()
    {
        sqlist L;
        Initlist (L);
        printf("%s",L.elem[0].name);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月7日
  • 已采纳回答 4月29日
  • 创建了问题 4月29日

悬赏问题

  • ¥15 关于#c语言#的问题,请各位专家解答!
  • ¥15 这个如何解决详细步骤
  • ¥15 在微信h5支付申请中,别人给钱就能用我的软件,这个的所属行业是啥?
  • ¥30 靶向捕获探针设计软件包
  • ¥15 别人给钱就能用我的软件,这个的经营场景是啥?
  • ¥15 react-diff-viewer组件,如何解决数据量过大卡顿问题
  • ¥20 遥感植被物候指数空间分布图制作
  • ¥15 安装了xlrd库但是import不了…
  • ¥20 Github上传代码没有contribution和activity记录
  • ¥20 SNETCracker