c - 函数头/声明

标签 c function struct

这是我的主要功能以及我要传递的内容。

int main(void){
   struct can elC[7];    // Create an array of stucts 

   Initialize(elC); // initializes the array

   return 0;
}

int Initialize(struct can elC[7]){

}

在 C 中,我们不需要在 main 或其他东西之前声明函数吗?如果是的话,它看起来会怎么样?我的代码运行良好,声明为

int Initialize();

但是我不需要类似的东西

int Initialize(struct can elc[7]);

最佳答案

/* Declaration of Structure */
struct can {
   /* Structure members */
};

/* Function Prototype - ANY OF THE THREE */
int Initialize();
int Initialize(struct can []);
int Initialize(struct can var[]);

int main (void)
{
   struct can elC[7];   // Create an array of stucts 
   Initialize(elC);     // Call to function - Initializes the array
   return 0;
}

int Initialize(struct can elC[7])
{
        //Do something;
        return 0;
}

如果不声明原型(prototype)会发生什么

下面的内容就可以正常工作。

$ gcc filename.c   

当与警告的-Wall选项结合使用时,它将引发警告。

$ gcc filename.c -Wall
In function ‘main’:
Warning: implicit declaration of function ‘Initialize’ [-Wimplicit-function-declaration]

因此,最好在 main 之前声明原型(prototype),例如

int Initialize(struct can []);   

int Initialize(struct can var[]);

下面也是有效的,意味着您可以传递任意数量的参数。请参阅here

int Initialize();   

关于c - 函数头/声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656180/

相关文章:

c - 使用 autotools 构建动态程序列表

c - 如何在 Linux 中构建自动运行程序

c++ - 将 C++ 结构移植到 Delphi

c++ - C++ 类/结构成员的默认可见性

c++ - 如何使 find() 与一组结构一起工作?

c - AF_PACKET 套接字未接收 IPv6 数据包

c - 在C中制作二维数组的最佳方法是什么

c++ - c++中函数调用的问题

c# - 什么时候使用大括号,什么时候不用?

Javascript 函数执行和嵌套函数调用