小编Beg*_*ner的帖子

表达式必须是结构指针上可修改的左值

我试图用数组“temp fields”(包含字符串)中的值填充项目类型“temp”,我在 ptemp 指针上收到“表达式必须是可修改的左值”错误。

typedef struct _item {
   char item_name[ITEM_NAME_LENGTH];
   char department[DEPARTMENT_NAME_LENGTH];
   char expiration_date[EXPIRATION_DATE_STRING_LENGTH];
   double price;
   int available;} Item;

Item create_item(char *s) {
    Item temp, *ptemp;
    ptemp = &temp;
    char temp_fields[5];
    int n = 0;
    n = seperate_fields(s, "_*_", temp_fields);
    ptemp->item_name = temp_fields[0];
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么会发生这种情况?我试图修改指针指向的值。应该是可以修改的

我感谢任何提前回答的人

编辑后的项目创建代码

Item create_item(char *s) {
Item temp, *ptemp;
char *ptrend;
char *temp_fields[5];
int n = 0;
ptemp = &temp;
n = seperate_fields(s, "_*_", temp_fields);
strcpy(ptemp->item_name, temp_fields[0]);
strcpy(ptemp->department,temp_fields[1]);
strcpy(ptemp->expiration_date, temp_fields[2]);
ptemp->price = strtod(temp_fields[3], &ptrend);
ptemp->available = …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

pointers ×1

struct ×1