I'm a rather experience C++ programmer, but with (pure!?) C-code I am sometimes struggling, especially when I haven't used it in a while. Can someone give me a hint on why the code below is not working? Compiler says:
syntax error : missing ')' before '&'
complaining about the line that has the printItem-signature. How I can make it work? Is the only option to change the printItem-signature to:
void printItem(const struct structItem *const item)
{
printf("age: %i, price: %i", item->age, item->price);
}
Or is there another way, that would allow me to keep the signature as it is below? Also, ...I didn't use the more convenient "typedef" notation for the struct, because I remember some issues with that - possibly in this context. If anyone could shed some light on all this, I would be very thankful.
#include <stdio.h>
struct structItem {
int age;
int price;
};
void printItem(const struct structItem &item)
{
printf("age: %i, price: %i", item.age, item.price);
}
int main()
{
struct structItem item;
item.age = 1;
item.price = 200;
printItem(item);
getchar();
return 0;
}
In C, you must pass by pointers so you would define printItem
like this:
void printItem(struct structItem *item)
{
printf("age: %i, price: %i", item->age, item->price);
}
To call it, give it the pointer. E.g.:
printItem(&item);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With