Link to c-code for structure

I am rather an experienced C ++ programmer, but with (clean !?) C code, which I sometimes come across, especially when I have not used it after a while. Can someone give me a hint why the code below is not working? The compiler says:

syntax error : missing ')' before '&' 

complains about a string that has a printItem signature. How can I make it work? The only way 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 allows me to keep the signature, as it is below? Also ... I didn’t use the more convenient β€œtypedef” notation for structure because I remember some problems with this - perhaps in this context. If anyone could shed light on all this, I would be very grateful.

 #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; } 
+4
source share
6 answers

Links are C ++ syntax. In pure C, you can use call-by-reference with pointers.

 void printItem(const struct structItem* const item) { printf("age: %i, price: %i", item->age, item->price); } 

And be careful with calling the function. You need to call him with the address of the structure.

 struct structItem item; item.age = 1; item.price = 200; printItem(&item); 
+2
source

In C, you must follow pointers to define printItem as follows:

 void printItem(struct structItem *item) { printf("age: %i, price: %i", item->age, item->price); } 

To call it, enter the pointer. For instance:.

 printItem(&item); 
+4
source
 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; } 
+2
source

Links are not part of the C syntax, so you have to change it to the signature void printItem(const struct structItem *const item) specified in the question, and change the call accordingly, i.e. printItem(&item);

Even John Skeet says this: Does C have links?

+1
source

With a small structure that you can pass by value:

 void printItem(struct structItem item) { printf("age: %i, price: %i\n", item.age, item.price); } 

This happens faster, since dereferencing is not required, and passing by value makes it clear that the element will not be changed.

0
source

The problem that apparently causes the assembly to break,

 void printItem(const struct structItem *const item) 

There is no reason to use the const keyword twice; in fact, it is illegal. Even if conforming to cranked up standards, I can still compile

 #include <stdio.h> struct structItem { int age; int price; }; void printItem(const struct structItem *item) { printf("age: %i, price: %i\n", item->age, item->price); } int main(void) { struct structItem item; item.age = 10; item.price = 100; printItem(&item); return 0; } 

no problem and the program prints

 age: 10, price: 100 

as was expected.

-one
source

Source: https://habr.com/ru/post/1397516/


All Articles