English 中文(简体)
发病结构
原标题:foward typedef structures
gcc 4.4.4 c89

我的头脑中就这样了。

港口。

struct struct_tag;

int initialize_ports(struct_tag *port);

在我的假肢档案中,我有:

港口c

typedef struct struct_tag {
    int port_id;
} Port_t;

在我的司机档案中,我有:

#include "港口。"
int initialize_ports(struct_tag *port)
{
    port = malloc(sizeof *port);
    /* do checking here */
}

我已宣布这一结构,因为我想掩盖内部因素。

然而,在我最初的人物——在头号档案中的港口——上,我正发现以下错误:

expected ‘)’ before ‘*’ token

我很想知道,我怎么能够宣布并能够把结构当作一个参数?

许多人感谢任何建议,

最佳回答

如其他答复所示,请could。 改动struct_tag_struct_tag。 编辑贵国法典的另一个方式是写字

typedef struct struct_tag struct_tag;

取代您现有的<代码>结构-标签;(即将类型与前方定义相结合)。 这样,你就可以写字。

int initialize_ports(struct_tag *port)

without compile failures. However, this is still not quite what you want, because the caller can neither allocate a local variable of this type, nor malloc() one - because they don t know the size.

另一些答复则建议,你应开放结构的定义。 这通常不是正确的答案,因为它排除了你试图制造的抽象层面。 更好地履行职能(在<代码>port.c,即does/em>的图书馆)。 内部知识,例如:

struct_tag *create_port(...);
void free_port(struct_tag *port)

i. 创建和免除结构,实际上为其他业务(例如从/写作到结构阅读)创建和自由。

问题回答

您应使用:

int initialize_ports(struct struct_tag *port);
                     ^^^^^^

另外,提前申报给你一份不完整的,你不知道大小。 如果您需要分配<条码> 构件_tag,则需要列入这一条的全部定义。 或者,如果你想使其完全不透明,你可以使用<代码>create_struct_tag()功能。

如果你不把“港口”的规模推向KNOW,那么你就错了,因为必须做的是先期申报。

简言之,你最好不要在这里使用先期声明,除非你也定出一种不变的价值,即“结构-标签”的规模。 你最有可能完全宣布这一点。

<sizeof的操作者在compile time上不停运行,按行:

port = malloc(sizeof *port);

汇编者没有关于结构规模的信息。

解决办法包括:

  • fully define the type in the header file.
  • define initialize_ports() in port.c after the struct is fully defined.
  • have initialize_ports() call a function defined in ports.c to get the size of Port_t at run-time.

无论如何,请not界定 初始化_ports ( in the Header file power.h,除非您的汇编者支持inline_inline, 关键词。 然而,这种使用将使该守则不符合ISO C标准,因此,由于C++对关键词的标准支持,因此,如果你不使用过于严格的合规选择,你可能会发现该守则是大多数C工具链的延伸,包括C++汇编。

然而,你所看到的错误信息是出于不同的原因。 与C++不同,struct_tag 仅此就不代表一种类型(如果你不需要<条形码>><>>>> > >,你必须使用<条码><>truct 关键词。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签