加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 指向程序的指针

发布时间:2020-12-16 10:22:40 所属栏目:百科 来源:网络整理
导读:是否可以通过指针调用过程?我在互联网上没有找到任何关于它的信息,但是下面的实验代码在没有警告的情况下编译. #include iostream#include ctimeusing namespace std;void PrintCurrentClock(){ coutclock()endl;}void PrintCurrentTimeStamp(){ couttime(0
是否可以通过指针调用过程?我在互联网上没有找到任何关于它的信息,但是下面的实验代码在没有警告的情况下编译.

#include <iostream>
#include <ctime>

using namespace std;

void PrintCurrentClock()
{
    cout<<clock()<<endl;
}

void PrintCurrentTimeStamp()
{
    cout<<time(0)<<endl;
}


int main()
{
    void* pF = PrintCurrentClock;

    pF;

    pF = PrintCurrentTimeStamp;

    pF;

    system("Pause");
    return 0;
}

输出为空,好像* pF有些“透明”.

解决方法

C和C都有函数指针,可以让你做你想要的:

void (*pf)(void);
pf = FunctionWithVeryVeryVeryLongNameThatPrintsTheCurrentClockStateUsingStdCoutOutputStream;
pf();

括号中的空格是可选的.

您没有找到关于该主题的任何内容的原因是,由于历史原因,C中的函数和过程都被称为函数(原因是原始语言中没有void – 过程默认返回int,并且返回值被忽略). C继承了这个命名约定.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读