gpt4 book ai didi

c - 将结构项作为参数传递

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:24 24 4
gpt4 key购买 nike

我知道可以将结构作为参数等传递。

但是是否可以预先设置参数,以便只能传递特定的结构项:

struct inventory* searchForItem(struct stockItem.componentType code){

我得到:错误:预期';' , ', ' 标记前

编辑:

typedef struct stockItem {
char *componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;

最佳答案

(因为对其他答案的评论太局限了)

首先为componentType 定义一个新类型,如下所示:

typedef char *stockItem_componentType; // Naming subject to conventions

现在在您的结构中,您使用此类型而不是简单的 char*。这是可选的,但非常推荐。像这样:

typedef struct stockItem {
stockItem_componentType componentType;
char *stockCode;
int numOfItems;
int price;
} stockItem;

最后,你的函数原型(prototype)是:

struct inventory* searchForItem(stockItem_componentType code);

关于c - 将结构项作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com