使用struct数组指针作为参数的函数,C语言。

时间:2022-09-06 11:19:27
typedef struct {
    char manufacturer[SIZE];
    char model[SIZE];
    int size;
    int id;
    int qty;
    double cost;
    double price;
} tv;    

void firstSix(tv *tvarr[]);
void firstSix(tv *tvarr[])
{
    (*tvarr[0]).manufacturer = "Vizio";
}

I am making an inventory program. It consists of an array of structs that will store information about different televisions. In my program I am required to hardcode six entries into the array, so I am trying to make a function that will take a struct array pointer argument. In the above code, I included the struct declaration, the function prototype and function definition that I am trying to make. Everything is placed before and after main in the respective order. I don't understand why Visual Studio is highlighting the first parenthesis in the code inside the function definition and saying "expression must be a modifiable lvalue". I don't understand what it is that I am doing wrong. Please help.

我正在做一个库存计划。它由一系列结构体组成,这些结构体将存储关于不同电视的信息。在我的程序中,我被要求对数组中的六个条目进行硬编码,所以我正在尝试创建一个函数,它将接受一个struct array指针参数。在上面的代码中,我包含了struct声明、函数原型和函数定义。所有东西都按照各自的顺序放在main之前和之后。我不明白为什么Visual Studio在函数定义中突出显示代码中的第一个括号,并说“表达式必须是可修改的lvalue”。我不明白我做错了什么。请帮助。

2 个解决方案

#1


2  

You cannot assign an array like that. You need to do

不能像这样分配数组。你需要做的

strcpy ((*tvarr[0]).manufacturer, "Vizio");

strcpy((* tvarr[0])。制造商,“Vizio”);

Make sure that you don't go out of bounds when copying the string into the array.

在将字符串复制到数组时,请确保不会超出范围。

You can either check the size of the string in advance or use strncpy which will limit the maximum number of characters to be copied.

您可以预先检查字符串的大小,或者使用strncpy来限制要复制的字符的最大数量。

An array is not a modifiable l-value. So basically you cannot have it on the left hand side of an assignment.

数组不是可修改的l值。所以基本上你不能把它放在作业的左边。

Or may be you also might want to define manufacture as char *manufacture and then dynamically allocate the string.

或者您也可能想定义制造为char *制造,然后动态地分配字符串。

manufacturer = strdup ("Vizio"); //manufacturer is char *

制造商=第6行的“Vizio”);/ /制造商是char *

Or depending on the length first allocate the buffer

或者取决于首先分配缓冲区的长度

manufacturer = malloc (sizeof (char) * needed_bytes);

制造商= malloc (sizeof (char) * needed_bytes);

Whenever you dynamically allocate the buffer, whenever you have finished working with it always remember to free it free (manufacturer).

无论何时动态分配缓冲区,无论何时使用完缓冲区,都要记得释放缓冲区(制造商)。

#2


2  

I think you want to do something like

我认为你想做一些类似的事情

strncpy((tvarr[0])->manufacturer, "Vizio", SIZE - 1);

Kevin has it; you can't assign a string to a pointer, you must copy the data to the array. I suggest strncpy to keep from running off the end of the allocated space.

凯文已经;不能将字符串分配给指针,必须将数据复制到数组中。我建议strncpy避免耗尽分配空间的末尾。

#1


2  

You cannot assign an array like that. You need to do

不能像这样分配数组。你需要做的

strcpy ((*tvarr[0]).manufacturer, "Vizio");

strcpy((* tvarr[0])。制造商,“Vizio”);

Make sure that you don't go out of bounds when copying the string into the array.

在将字符串复制到数组时,请确保不会超出范围。

You can either check the size of the string in advance or use strncpy which will limit the maximum number of characters to be copied.

您可以预先检查字符串的大小,或者使用strncpy来限制要复制的字符的最大数量。

An array is not a modifiable l-value. So basically you cannot have it on the left hand side of an assignment.

数组不是可修改的l值。所以基本上你不能把它放在作业的左边。

Or may be you also might want to define manufacture as char *manufacture and then dynamically allocate the string.

或者您也可能想定义制造为char *制造,然后动态地分配字符串。

manufacturer = strdup ("Vizio"); //manufacturer is char *

制造商=第6行的“Vizio”);/ /制造商是char *

Or depending on the length first allocate the buffer

或者取决于首先分配缓冲区的长度

manufacturer = malloc (sizeof (char) * needed_bytes);

制造商= malloc (sizeof (char) * needed_bytes);

Whenever you dynamically allocate the buffer, whenever you have finished working with it always remember to free it free (manufacturer).

无论何时动态分配缓冲区,无论何时使用完缓冲区,都要记得释放缓冲区(制造商)。

#2


2  

I think you want to do something like

我认为你想做一些类似的事情

strncpy((tvarr[0])->manufacturer, "Vizio", SIZE - 1);

Kevin has it; you can't assign a string to a pointer, you must copy the data to the array. I suggest strncpy to keep from running off the end of the allocated space.

凯文已经;不能将字符串分配给指针,必须将数据复制到数组中。我建议strncpy避免耗尽分配空间的末尾。