int main() { struct structItem item; item.age = 1; item.price = 200; printItem(item);
At this point, you pass the entire structure to the printItem() function. The compiler (semantically, if not in fact) copies the structure to temporary storage for your function. If you want to pass the address of the item structure, change the call:
printItem(&item);
In any case, you will need to change the prototype for printItem() , as this is not legal. C:
void printItem(const struct structItem &item)
GCC gives an error message:
$ make item cc item.c -o item item.c:8:40: error: expected ';', ',' or ')' before '&' token
Change the & parameter to * in the function prototype to declare that the function accepts a pointer to structItem , change the function to use -> to access structure elements via a pointer, and the full program:
#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; }
source share