Reputation: 7958
I'm working on a c program and here is the structure I am using
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 ,
error: invalid type argument of ‘unary *’ (have ‘int’)
Invalid type argument of -> C structs
but they don't seem to help me.
Any suggestions on how to solve the problem ?
Upvotes: 2
Views: 8855
Reputation: 4523
no need *
for new->quantity
the ->
is shortcut for (*new).quantity
Upvotes: 1
Reputation: 9029
The ->
operator dereferences the pointer, so use of additional *
is not required. And results in error.
Upvotes: 1
Reputation: 39950
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.
Upvotes: 6
Reputation: 437376
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.
Dereferencing the new
pointer (is it even legal to have a variable named such?) is already done through operator->
.
Upvotes: 4