[英]C : Working with structures and pointers , error : invalid type argument of unary ‘*’
I'm working on ac program and here is the structure I am using 我正在研究ac程序,这是我正在使用的结构
struct EngineParts{
int serial_number;
unsigned int year_of_manufacture;
unsigned int quantity;
char *material;
}*Eparts;
And I am getting the following error 我收到以下错误
`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’)`
in these three lines 在这三行中
*new->quantity = quantity;
*new->serial_number = serial_number;
*new->year_of_manufacture = year_of_manufacture;
Here is the complete implementation 这是完整的实现
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++;
}
PS : I have checked out the following questions , PS:我查看了以下问题,
error: invalid type argument of 'unary *' (have 'int') 错误:'unary *'的无效类型参数(有'int')
Invalid type argument of -> C structs 无效的类型参数 - > C结构
but they don't seem to help me. 但他们似乎没有帮助我。
Any suggestions on how to solve the problem ? 有关如何解决问题的任何建议?
The ->
operator already dereferences the pointer for you. ->
运算符已经为您取消引用指针。
new->serial_number
is equivalent to (*new).serial_number
, both of which seem like what you want. new->serial_number
相当于(*new).serial_number
,两者看起来都像你想要的。
Do it this way: 这样做:
new->quantity = quantity;
The extra pointer dereference ( *new->quantity
) is an error: new->quantity
is an int
, not any kind of pointer so the compiler complains. 额外的指针取消引用( *new->quantity
)是一个错误: new->quantity
是一个int
,而不是任何类型的指针,因此编译器会抱怨。
Dereferencing the new
pointer (is it even legal to have a variable named such?) is already done through operator->
. 取消引用new
指针(甚至是合法的,有一个名为so的变量?)已经通过operator->
完成了。
The ->
operator dereferences the pointer, so use of additional *
is not required. ->
运算符取消引用指针,因此不需要使用额外的*
。 And results in error. 并导致错误。
no need *
for new->quantity
不需要*
为new->quantity
the ->
is shortcut for (*new).quantity
->
是(*new).quantity
快捷方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.