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

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

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;
}


  • 使用 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;
}