免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12670 | 回复: 11
打印 上一主题 下一主题

[函数] 关于函数参数(void与空) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-16 12:06 |只看该作者 |倒序浏览
刚才在看C与C++之争的那个帖子,有一位朋友指出一个问题,认为这是C不严谨的地方

  1. #include <stdio.h>;
  2. int f();

  3. int main()
  4. {
  5.         int i=10;
  6.         f();
  7.         f(i);
  8.         system("pause");
  9.         return 0;
  10. }

  11. int f(int i)
  12. {
  13.         printf("i=%d\n",i);
  14.         return 0;
  15. }
复制代码

记得我以前学这个的时候,是这样理解的:
f(void)指定零参数,f()则对应不确定个数的参数。
如果用函数指针来理解,应该好说一些(当然我在这个程序中都用了不恰当的调用):

  1. #include <stdio.h>;
  2. int f(int);
  3. int f2(int, int);

  4. int main()
  5. {
  6.         int i=10, (*p)();
  7.         p=f;
  8.         p();
  9.         p=f2;
  10.         p(i);
  11.         system("pause");
  12.         return 0;
  13. }

  14. int f(int i)
  15. {
  16.         printf("i=%d\n",i);
  17.         return 0;
  18. }

  19. int f2(int i, int j)
  20. {
  21.         printf("%d\t%d\n", i, j);
  22. }

复制代码

我觉得这个特性只有在用于函数指针的时候才有意义。
平时进行函数声明的时候,无参数的函数应该声明成f(void)
C99 P119:
10 The special case of an unnamed parameter of type void as the only item in the list
specifies that the function has no parameters.


但是同样在C99 P120中:
16: The declaration
int f(void), *fip(), (*pfi)();
declares a function f with no parameters returning an int, a function fip with no parameter specification
returning a pointer to an int, and a pointer pfi to a function with no parameter specification returning an
int.

我在C99标准中没有找到f()声明有匹配任意参数作用的话。
不知道这个是标准(可能我没找对地方),还只是GCC的扩展?
请大家指教。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-03-16 12:51 |只看该作者

关于函数参数(void与空)

不写void是K&R C中的。这个地方可以说是C的一个缺陷。但是现在,在任何一本书中都推荐使用void的写法,《C专家编程》中专门有提到这个。

只是现在各个标准都兼容以前的这种K&R写法。是兼容,已经不推荐使用了。

就好像在K&R C中,函数都是这样定义的:

  1. int i;
  2. void fun(i)
  3. {
  4.         printf("%d\n", i);
  5.         return ;
  6. }
复制代码

现在已经不推荐使用了,但大多数编译器仍能通过而不报任何错误或者警告。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2005-03-16 13:11 |只看该作者

关于函数参数(void与空)

兄弟,你写错了。应该是:
  1. void fun(i)
  2. int i;
  3. {
  4.    ...
  5. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2005-03-16 13:15 |只看该作者

关于函数参数(void与空)

sigh,第一次写对了。觉得不对,又编辑错了。唉,老东西了,老也不用了。

^_^,大家更要从这个例子上引以为戒哦。

论坛徽章:
0
5 [报告]
发表于 2005-03-16 15:33 |只看该作者

关于函数参数(void与空)

aero 又显老了,那还戴个小军帽,返老还童了

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2005-03-16 15:39 |只看该作者

关于函数参数(void与空)

[quote]原帖由 "lchhcllch"]aero 又显老了,那还戴个小军帽,返老还童了[/quote 发表:


俺是说这种写法太老了。^_^。

论坛徽章:
0
7 [报告]
发表于 2005-03-16 15:40 |只看该作者

关于函数参数(void与空)

我记得看B.S访谈的时候,他提到过这个问题。当初c++设计时关于是否用f(void)表示无参数,争取过c语言一个代表人物的意见(具体是谁忘了)。那个人当时强烈反对,说这样的语法非常丑陋。c++就没用。结果后来,c用了……

论坛徽章:
0
8 [报告]
发表于 2005-03-16 15:43 |只看该作者

关于函数参数(void与空)

楼主把那个函数的参数改成其他类型的看看,是否能够通过编译。

论坛徽章:
0
9 [报告]
发表于 2005-03-16 15:47 |只看该作者

关于函数参数(void与空)

我想知道的是:
我在C99标准中没有找到f()声明有匹配任意参数作用的话。
不知道这个是标准(可能我没找对地方),还只是GCC的扩展?
请大家指教。

F()这样的写法,究竟是天使,还是恶魔?
呵呵,我觉得是孙猴子的金箍棒。

论坛徽章:
0
10 [报告]
发表于 2005-03-16 16:16 |只看该作者

关于函数参数(void与空)

参考一下:)
引用网址:http://david.tribble.com/text/cdiffs.htm#C99-func-vararg


Empty parameter lists
C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of only void. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.

    // C code

    extern int  foo();          // Unspecified parameters
    extern int  bar(void);      // No parameters

    void baz()
    {
        foo(0);         // Valid C, invalid C++
        foo(1, 2);      // Valid C, invalid C++

        bar();          // Okay in both C and C++
        bar(1);         // Error in both C and C++
    }
C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.

    // C++ code

    extern int  xyz();

    extern int  xyz(void);  // Same as 'xyz()' in C++,
                            // Different and invalid in C
For code that is intended to be compiled as either C or C++, the best solution to this problem is to always declare functions taking no parameters with an explicit void prototype. For example:

    // Compiles as both C and C++
    int bosho(void)
    {
        ...
    }
Empty function prototypes are a deprecated feature in C99 (as they were in C89).

[C99: §6.7.5.3]
[C++98: §8.3.5, C.1.6.8.3.5]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP