int func()v / s int func(void) [英] int func() v/s int func(void)

查看:120
本文介绍了int func()v / s int func(void)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我在几篇帖子前看到这些不等同,而且

前者意味着''没有指明参数''并且要求

实例func(3,THREE,& myint)是合法的。


那是什么用途声明功能的方式?


谢谢,

V.

Hello,

I read a couple of posts ago that these are not equivalent, and that
the former means ''no arguments specified'' and that calling for
instance func(3,"THREE",&myint) is legal.

What''s the intended use of that way of declaring functions?

Thanks,
V.

推荐答案

Vinko Vrsalovic < 6 **** @ gmail.com>在消息中写道

news:8c ************************** @ posting.google.c om ...
"Vinko Vrsalovic" <vi****@gmail.com> wrote in message
news:8c**************************@posting.google.c om...
Re:int func()v / s int func(void)


(如果您的主题文本是作为

你的留言,把它放在你的留言里。不是每个人的

新闻阅读器同时显示主题和身体,

在这种情况下失去了。)

你好,

我读了几篇文章说这些都不相同,


正确。

和那个
前者意味着''没有指明参数''


正确。 (void)意思是没有参数。

并且要求
实例func(3,THREE,& myint)是合法的。


它''合法''但可能没有'工作''(如果使用不正确,可能产生未定义的

行为 - 即函数必须写入
才能处理这些参数。

这种声明函数的方式的用途是什么?
Re: int func() v/s int func(void)
(If the text of your subject is intended as part of
your message, put it in your message. Not everyone''s
newsreader shows the subject and body simultaneously,
in which case context is lost.)
Hello,

I read a couple of posts ago that these are not equivalent,
Correct.
and that
the former means ''no arguments specified''
Correct. (void) means specifically ''no arguments''.
and that calling for
instance func(3,"THREE",&myint) is legal.
It''s ''legal'' but might not ''work'' (could produce undefined
behavior if not used correctly -- i.e. the function must
be written to handle those arguments).
What''s the intended use of that way of declaring functions?




编写易碎代码。我不要那样做。从原型被添加到C的前几天开始,它就是一个不合时宜的
时间错误。(它已被保留以便旧代码将

仍然编译)。原型通过使编译器能够做更好的错误

检查,帮助您创建正确的
代码。使用它们。永远。

-Mike



To write fragile code. IOW Don''t Do That. It''s an
anachronism from the days before prototypes were
added to C. (It''s been kept so that old code will
still compile). Prototypes help you create correct
code, by enabling the compiler to do better error
checking. Use them. Always.
-Mike


Vinko Vrsalovic< vi **** @ gmail.com>写道:


请把你正在谈论的内容放入邮件正文中,

主题行被新闻阅读器截断,制作什么

你问的很难甚至不可能理解......


主题:int func()v / s int func(void)
Vinko Vrsalovic <vi****@gmail.com> wrote:

Please put what you''re talking about into the body of your mail,
subject lines get ofter truncated by the news reader, making what
you ask about hard or even impossible to understand...

Subject: int func() v/s int func(void)
我在几篇帖子之前看到这些不等同,而且前者意味着''没有指定参数''并且要求
实例func(3, THREE,& myint)是合法的。
这种声明函数的方式的用途是什么?
I read a couple of posts ago that these are not equivalent, and that
the former means ''no arguments specified'' and that calling for
instance func(3,"THREE",&myint) is legal. What''s the intended use of that way of declaring functions?




它使编译器不会检查你是否使用了函数 - br />
定义的参数和类型数量。一个预期的使用我能想到

是用于ANSI之前的程序(大约在1989年之前编写)你没有使用

函数原型但是你仍然想要现代编译器能够编译
。另一个可能的用途是在插件系统中声明函数指针

,其中参数的数量在编译时不知道,但是在库可以在程序已经运行时加载,并且你有方法可以确定函数接受多少(或哪些)参数

(然后相应地调用函数) 。例如。你可以拥有(非标准的,可能是系统特定的)函数

pointer_to_func()和required_arg_count()这样使用


int(* to_be_loded)(); / *参数未指定! * /

int num_args;

int result;


to_be_loaded = pointer_to_func(" name_of_function_to_be_loaded");

num_args = required_arg_count(" name_of_function_to_be_loaded");


if(num_args == 0)

result = to_be_loaded();

else if(num_args == 1)

result = to_be_loaded(42);

else

{

fprintf(stderr,需要太多参数\ nn);

退出(EXIT_FAILURE);

}

此致,Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


Vinko Vrsalovic写道:
Vinko Vrsalovic wrote:
你好,

我读了几篇帖子之前那个ese不等同,而前者意味着''没有指明参数''并且要求
实例func(3,THREE,& myint)是合法的。

这种声明函数的用途是什么?
Hello,

I read a couple of posts ago that these are not equivalent, and that
the former means ''no arguments specified'' and that calling for
instance func(3,"THREE",&myint) is legal.

What''s the intended use of that way of declaring functions?




这是C早期开发的遗留问题,

保留,因此在创建

ANSI标准之前编写的代码有希望继续正常工作。


旧时(&b) ; K& R)C没有功能原型;所有你

可以说关于其他地方定义的功能就是它

返回了这样一种类型。除了实际的

函数定义之外,你无法描述

数和参数类型,你这样做是这样的:


int func(a,b,c)

int a;

char * b;

int * c;

{

/ *做东西* /

返回42;

}


....在其他一些模块中你会把它声明为


int func();



extern int func();


....所以没有在

声明中给出有关参数的信息。

ANSI C从C ++中汲取了一个想法,允许更详细的功能; b $ b规范的功能;这个想法很好,所有新的(即最近十年或十二年)应该使用

更新的表格。尽管如此,旧表格仍然以

向后兼容的名义保留。 (一个名字不容小觑,我可能

补充:C已经标准化了不到一半的生命,并且

来自它的相当多的代码青少年时期仍然存在。)


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子



It''s a leftover from the early development of C,
retained so code written prior to the creation of the
ANSI Standard had a hope of continuing to work correctly.

Old-time ("K&R") C had no function prototypes; all you
could say about a function defined "elsewhere" is that it
returned such-and-such a type. You could not describe the
number and types of the arguments except in the actual
function definition, which you did like this:

int func(a, b, c)
int a;
char *b;
int *c;
{
/* do stuff */
return 42;
}

.... and in some other module you''d declare it as

int func();
or
extern int func();

.... so no information about the arguments was given in the
declaration.

ANSI C swiped an idea from C++ to allow much more detailed
specification of functions; the idea was a good one, and all
new (i.e., for the last ten or twelve years) should use the
newer form. Still, the old form is kept around in the name of
backwards compatibility. (A name not to be trifled with, I might
add: C has been Standard-ized for less than half its life, and
quite a lot of code from its adolescent years is still around.)

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于int func()v / s int func(void)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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