gpt4 book ai didi

c - 为什么 C 中函数的原型(prototype)和定义可能不同?

转载 作者:太空狗 更新时间:2023-10-29 16:27:31 26 4
gpt4 key购买 nike

我想知道为什么这会编译:

int test();

int main() { return test((void*)0x1234); }
int test(void* data) { return 0; }

为什么编译器不会发出任何错误/警告(我试过 clang、gcc)?如果我更改返回值,它不会编译 - 但参数可能不同?!

最佳答案

如果你改变:

int test();

到:

int test(void);

你会得到预期的错误:

foo.c:4: error: conflicting types for ‘test’
foo.c:1: error: previous declaration of ‘test’ was here

这是因为 int test(); 只是简单地声明了一个接受任何参数的函数(因此与你随后定义的 test 兼容) >),而 int test(void); 是一个实际的函数原型(prototype),它声明了一个函数,该函数没有参数(并且兼容与后续定义)。

关于c - 为什么 C 中函数的原型(prototype)和定义可能不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11646278/

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