gpt4 book ai didi

C : Working with structures and pointers , 错误:一元 ‘*’ 的无效类型参数

转载 作者:行者123 更新时间:2023-12-04 21:04:00 24 4
gpt4 key购买 nike

我正在编写一个 C 程序,这是我正在使用的结构

struct EngineParts{

int serial_number;
unsigned int year_of_manufacture;
unsigned int quantity;
char *material;

}*Eparts;

我收到以下错误

`Automobile.c:79:5: error: invalid type argument of unary ‘*’ (have ‘unsigned int’)` 

`Automobile.c:80:5: error: invalid type argument of unary ‘*’ (have ‘int’)`

`Automobile.c:81:5: error: invalid type argument of unary ‘*’ (have ‘unsigned int’)`

在这三行

 *new->quantity = quantity;
*new->serial_number = serial_number;
*new->year_of_manufacture = year_of_manufacture;

这里是完整的实现

void automobile_add_part(const int serial_number,
const unsigned int year_of_manufacture,
const unsigned int quantity ,
const char *material){
/*
* Store the address to the latest part
*/
struct EngineParts *new ;
new = (Eparts+available_number_of_parts);

// Copying a String is a little bit complicated
// First memory is allocated for the string
*new->material = (char *)calloc(strlen(material),sizeof(char));
//Then the string is copied
strcpy((char *)*new->material,material);

*new->quantity = quantity;
*new->serial_number = serial_number;
*new->year_of_manufacture = year_of_manufacture;

available_number_of_parts++;
}

附言:我检查了以下问题,

error: invalid type argument of ‘unary *’ (have ‘int’)

Invalid type argument of -> C structs

但他们似乎并没有帮助我。

有什么解决问题的建议吗?

最佳答案

-> 运算符已经为您解除了对指针的引用。

new->serial_number 等同于 (*new).serial_number,两者看起来都是您想要的。

关于C : Working with structures and pointers , 错误:一元 ‘*’ 的无效类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7697783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com