与函数声明混淆 [英] confused with function declarations

查看:89
本文介绍了与函数声明混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


我对C中的原型感到困惑。我在C书中看到了以下代码:

void init_array_1(int data [ ])

{

/ *这里的一些代码* /

}

void init_array_2(int * data_ptr)

{

/ *这里有些代码* /

}


int main()

{

int array [MAX];


void init_array_1();

void init_array_2( );


/ *某些代码无效* /


返回(0);

}


我不明白为什么我们需要两行函数声明

在main里面:

void init_array_1();

void init_array_2();


通常我会把它们放在main()之前,并在main之后放置那些函数

的定义。如果两个功能块已经放在

main之前,我认为不再需要功能声明了。


非常感谢。

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data[])
{
/* some code here */
}
void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don''t understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();

Normally I would put them before main() and put those function
definitions after main. If the two functions block have been put before
main, I would thought no function declarations are needed any more.

Thank you very much.

推荐答案

Xiaoshen Li写道:
Xiaoshen Li wrote:

亲爱的,

我我在C语言中与原型混淆。我在C书中看到了以下代码:

void init_array_1(int data [])
{
/ *这里的一些代码* /
}

void init_array_2(int * data_ptr)
{
/ *这里有些代码* /
}

()
{int array [MAX];

void init_array_1();
void init_array_2();

/ *代码ommited * /

返回(0);
}

我不明白为什么我们需要两行函数声明
里面main:
void init_array_1();
void init_array_2();


我们不是。

通常我会把它们放在main()之前,然后在main之后放置那些函数
定义。


我也是。

如果两个功能块已经放在
main之前,我认为不再需要函数声明了。

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:

void init_array_1(int data[])
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don''t understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();
We don''t.
Normally I would put them before main() and put those function
definitions after main.
So would I.
If the two functions block have been put before
main, I would thought no function declarations are needed any more.




你会想到的。


-

pete



You would have thought correctly.

--
pete




" Xiaoshen Li" < XL ** @ gmu.edu>在消息中写道

news:do ********** @ osf1.gmu.edu ...

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:do**********@osf1.gmu.edu...
亲爱的所有人,

我对C中的原型感到困惑。我在C书中看到了以下代码:

void init_array_1(int data [])
{
/ *这里的一些代码* /
}

void init_array_2(int * data_ptr)
{
/ *这里的一些代码* /
}
int main()
{int array [MAX];

void init_array_1();
void init_array_2();

/ *有些代码无效* /

返回(0);
}

我不明白为什么我们需要两行函数声明
main main:
void init_array_1();
void init_array_2();

通常我会把它们放在main()之前并放入那些函数定义
之后主要。如果两个函数块都放在main之前,我会认为不再需要函数声明了。
Dear All,

I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data[])
{
/* some code here */
}
void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don''t understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();

Normally I would put them before main() and put those function definitions
after main. If the two functions block have been put before main, I would
thought no function declarations are needed any more.




知道它会很有趣你正在使用哪本书,以及这个代码出现时讨论的主题是什么?


你发布的内容显示了两个函数*定义* - 在main之前 - 和

两个不同的预定义用于main中的相同功能。然而,

后者预先改变了事物的形状 - 正如*说*编译器'

不知道你要传递给这些运行时有两个函数。所以,

他们与外部定义[以及它们出现时隐含的

声明]有些冲突。

正如他们后来被编译器看到的那样,后者基本上预先确定

否决了之前看到的定义?


例如.. 。


:::这表示init_array_1希望将int []传递给它。


void init_array_1(int data [] )

{

}

int main(无效)

{


:::这说''忘记你听说过这个功能 - 你不知道

它会被传递!!!''
:::

void init_array_1();


char x;


:: :将一个char传递给init_array_1 - 它[编译器]最好不要

抱怨 - 因为我们告诉它忘记了东西!


init_array_1(x);

}


最后,通常会看到预先提前主要...可能因为

包含文件主要是提供这些,并且*通常*包括在任何

代码之前[虽然没有真正的原因要做到这一点 - 只要在使用之前就看到了

]。




It''d be interesting to know which book you were using, and what the subject
under discussion was when this code appeared.

What you''ve posted shows two function *definitions* - ahead of main - and
two differing predecs for those same functions inside of main. However, the
latter predecs change the shape of things - as the *say* that the compiler''s
no idea what you''re going to pass to these two functions at runtime. So,
they''re somewhat in conflict with the external definitions [and the implicit
declarations that these make - when they appeared].

As they''re seen later by the compiler, the latter predecs essentially
overrule the earlier definitions seen?

For example ...

:::This says that init_array_1 expects an int[] to be passed to it.

void init_array_1(int data[])
{
}
int main(void)
{

::: this says ''forget what you''ve heard about this function - you don''t know
what it''ll be passed!!!''
:::
void init_array_1();

char x;

::: pass a char to init_array_1 - it [the compiler] had better not
complain - as we told it to forget stuff!

init_array_1(x);
}

Lastly, it is more usual to see predecs ahead of main ... possibly because
include files mainly provude these, and are *normally* included ahead of any
code [although there''s no real reason to do this - just as long as things
are seen before they''re used].





" pete" < PF ***** @ mindspring.com>在消息中写道

news:43 *********** @ mindspring.com ...

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Xiaoshen Li写道:
Xiaoshen Li wrote:
亲爱的所有人,

我对C中的原型感到困惑。我在C书中看到了以下代码:

void init_array_1(int data [] )
{
/ *这里的一些代码* /
}

void init_array_2(int * data_ptr)
{
/ *一些代码这里* /
}
int main()
{int array [MAX];

void init_array_1();
void init_array_2();

/ *有些代码无效* /

返回(0);
}

我不喜欢我不明白为什么我们需要在main内部使用两行函数声明:
void init_array_1();
void init_array_2();

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:

void init_array_1(int data[])
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don''t understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();



我们不是。



We don''t.

通常我会将它们放在main()之前并将这些函数定义在main之后。
Normally I would put them before main() and put those function
definitions after main.



我也是。



So would I.

如果两个函数块已经放在
main之前,我认为不再需要函数声明。
If the two functions block have been put before
main, I would thought no function declarations are needed any more.



你会想到的。



You would have thought correctly.




我会说''你没有更好的事情可做!''[at 11.00我不喜欢

现实反思 - 所以,我不会说出来!


该死的,哎呀,Ctrl + Z


该死了!



I would say ''have you nothing better to do!'' [at 11.00pm] - but I hate
reality-reflection - so, I won''t say it!

Damn it, oops, Ctrl+Z

damn it again!


这篇关于与函数声明混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆