void func()函数和void func(void)函数的区别

例子1:

#include <stdio.h>
#include <stdlib.h>

void show1(int age)
{
    printf("Age: %d\n", age);
}

void show2(char* str)
{
    printf("Name: %s\n", str);
}

void show3(int age, int number, char* str)
{
    printf("Age: %d\n", age);
    printf("Number: %d\n", number);
    printf("Name: %s\n", str);
}

int main(int argc, char* argv[])
{
    typedef void (*ptr_fun)();
    ptr_fun pfunc;
    
    pfunc = show1;
    pfunc(23);
    printf("\n");

    pfunc = show2;
    pfunc("Jack");
    printf("\n");

    pfunc = show3;
    pfunc(23, 202, "Jack");

    return 0;
}

可以看到当我们定义了typedef void (*ptr_fun)();此类型的函数指针,表示它可以指向任意个数参数的函数。

 

例子2:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void show()
{
    printf("Argument count is variable!\n");
}

struct Student
{
    int age;
    int number;
};

int main(int argc, char* argv[])
{
    struct Student Mike = {12, 56};

    show(23);
    show(3, "Jack");
    show(Mike);

    return 0;
}

 

程序输出:

 

可以看到输出各种类型,任意的参数都是可以的。

 

 

例子3:

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void show(void)
{
    printf("No argument!\n");
}

struct Student
{
    int age;
    int number;
};

int main(int argc, char* argv[])
{
    struct Student Mike = {12, 56};

    show(23);
    show(Mike);
    show();

    return 0;
}

 

程序输出:

这里我们可看到void show(void)不接受任何参数,调用时只能写成show()。

 

转载于:https://www.cnblogs.com/Robotke1/archive/2013/05/14/3077496.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值