简体   繁体   English

函数头/声明

[英]Function header/declaration

This is my main function and what I am passing. 这是我的主要功能,也是我要传递的内容。

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]){

}

In C don't we have to declare the function before main or something? 在C语言中,我们不必在main或其他东西之前声明函数吗? If so how would it look? 如果是这样,它将如何? My code runs fine with the declaration of 我的代码运行良好,声明为

int Initialize();

But don't I need something like 但是我不需要

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;
}

What happens if you don't declare prototype 如果不声明原型会怎样?

The below will work just fine. 以下将正常工作。

$ gcc filename.c   

When combined with -Wall option for warnings, it will throw a warning. 当与-Wall选项结合使用时,会发出警告。

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

So its always better to declare the prototype before main like 因此,总是最好在main之前声明原型

int Initialize(struct can []);   

or 要么

int Initialize(struct can var[]);

Below is valid too, means that you can pass any number of arguments. 以下也是有效的,表示您可以传递任意数量的参数。 See here . 这里

int Initialize();   

In C89 you do not have to declare a function before calling it; 在C89中,您不必在调用函数之前先声明它; however if you do call an undeclared function, it is as if you declared the function as: 但是,如果您确实调用了一个未声明的函数,则就像您将该函数声明为:

int funcname();

The empty parentheses do not mean that the function takes no parameters; 空括号并不表示该函数不带参数。 it just means that our declaration is not a prototype . 这仅仅意味着我们的声明不是原型 The number and types of the parameters are unknown at this stage (but it isn't a function taking a variable argument list like printf() because even in C89, those must have a prototype in scope when they are used). 在此阶段,参数的数量和类型是未知的(但它不是像printf()这样的带有可变参数列表的printf()因为即使在C89中,使用它们时,它们的作用域也必须是原型)。

If the actual function returns something other than int , or you call it with the wrong type or number of parameters, or if any of the parameters have a different type after the default argument promotions are applied, then calling the function causes undefined behaviour. 如果实际函数返回的不是int ,或者使用错误的类型或数量的参数调用它,或者在应用默认参数提升后任何参数的类型不同,则调用该函数会导致未定义的行为。

Your code doesn't fall foul of any of those limitations, so your code is correct in C89. 您的代码不会受到任何这些限制的影响,因此您的代码在C89中是正确的。 However it's considered good style to use function prototypes anyway as it enables the compiler to detect all of those conditions and report an error. 但是,无论如何都使用函数原型被认为是一种很好的样式,因为它使编译器能够检测所有这些情况并报告错误。

In C99 you do have to declare the function before calling it. 在C99中,必须在调用函数之前先声明它。 An appropriate prototype would be: 合适的原型将是:

int Initialize(struct can *elC);

Also I would advise to use C99 or C11 if your compiler supports them. 如果您的编译器支持,我也建议您使用C99或C11。 Regarding the use of [] in a function parameter list, see here . 关于在功能参数列表中使用[]请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM