I understand it is possible to pass a struct as an argument etc.
But is it possible to have the parameter preset so only a specific struct item can be passed such:
struct inventory* searchForItem(struct stockItem.componentType code){
I'm getting : error: expected ';' , ', ' before token
EDIT:
typedef struct stockItem {
char *componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
The type of that component is char *
, so just make that the type of the parameter.
struct inventory* searchForItem(char *code){
If you want to make the type more strict, make a typedef for the field in question:
typedef char * stockItem_componentType;
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
struct inventory* searchForItem(stockItem_componentType code){
Note that this hides a pointer behind a typedef
which is not recommended. Then people reading your code (including yourself) won't know just by looking at it that it's a pointer, which can lead to confusion.
(Since the comments on the other answer are too constrained)
First you define a new type for componentType
, like this:
typedef char *stockItem_componentType; // Naming subject to conventions
Now in your structure, you use this type instead of simple char*
. This is optional, but very much recommended. Like this:
typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;
And finally, your function prototype is:
struct inventory* searchForItem(stockItem_componentType code);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.