结构体数组未定义
发布网友
发布时间:2022-04-21 19:31
我来回答
共2个回答
热心网友
时间:2022-07-13 02:42
在你程序中,下面的定义方法是不对的:
struct commodity
{
char* strType; /*指针*/
char* strName; /*指针*/
int nPrice;
}comm[]={
"日常用品", "牙刷", 2, /*由于strType和strName是指针,在没有申请内存前是不能赋值的*/
"日常用品", "牙膏", 3,
"日常用品", "毛巾", 5,
"日常用品", "脸盆", 12,
"文具", "铅笔", 1,
"文具", "圆珠笔", 2,
"文具", "钢笔", 10,
"文具", "水彩笔",12,
"球类", "篮球", 66,
"球类", "足球", 120,
"球类", "乒乓球", 3,
"球类", "羽毛球", 15,
};
可以改为如下:
#define TYPE_LEN 20/*类型长度*/
#define NAME_LEN 20/*名字长度*/
struct commodity
{
unsigned char strType[TYPE_LEN]; /*数组,strType不能是char型,应该用unsigned char型,原因与汉字编码有关,你可以查相关资料*/
unsigned char strName[NAME_LEN]; /*数组,strName不能是char型,应该用unsigned char型,原因与汉字编码有关*/
int nPrice;
}comm[]={
"日常用品", "牙刷", 2, /*由于strType和strName是数组,可以直接赋值了*/
"日常用品", "牙膏", 3,
"日常用品", "毛巾", 5,
"日常用品", "脸盆", 12,
"文具", "铅笔", 1,
"文具", "圆珠笔", 2,
"文具", "钢笔", 10,
"文具", "水彩笔",12,
"球类", "篮球", 66,
"球类", "足球", 120,
"球类", "乒乓球", 3,
"球类", "羽毛球", 15,
};
热心网友
时间:2022-07-13 02:42
CString是MFC里的类型