V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
itsuibi
V2EX  ›  Linux

APUE 中有段代码,关于 typedef 定义不是很理解,期待解惑谢谢。

  •  
  •   itsuibi · 2014-07-26 15:07:22 +08:00 via Android · 2693 次点击
    这是一个创建于 3555 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请问
    typedef int Myfunc("const char *, const struct stat *, int");
    static Myfunc myfunc;
    这段代码应该怎么理解呢?

    typedef int (*Myfunc)("const char *, const struct stat *, int");

    有什么区别呢?

    谢谢
    第 1 条附言  ·  2014-07-26 18:53:04 +08:00
    typedef int Myfunc(const char *, const struct stat *, int);
    static Myfunc myfunc;
    这段代码应该怎么理解呢?

    typedef int (*Myfunc)(const char *, const struct stat *, int);

    Note: 写错了多加了一对 双引号 ,下面如果加了*我能明白,不加*我就有点懵了
    3 条回复    2014-07-26 23:54:54 +08:00
    wy315700
        1
    wy315700  
       2014-07-26 19:11:45 +08:00
    Myfunc 是一个函数类型

    static的意思是本文件作用域
    zeayes
        2
    zeayes  
       2014-07-26 21:55:24 +08:00   ❤️ 1
    typedef int Myfunc(const char *, const struct stat *, int);
    // Myfunc是一个有3个参数(分别是:const char *, const struct stat *, int)且返回值是int的函数类型。
    static Myfunc myfunc;
    // 申明一个Myfunc类型的静态变量。

    typedef int (*Myfunc)(const char *, const struct stat *, int);
    // Myfunc是一个有3个参数(分别是:const char *, const struct stat *, int)且返回值是int的函数指针。
    baka
        3
    baka  
       2014-07-26 23:54:54 +08:00   ❤️ 1
    函数名或函数指针对编译器而言,结果实际上是一样的。
    以函数名调用的时候,编译器会做function to pointer conversion,这样还是会拿到函数入口指针,相当于做了&myfunc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   902 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:55 · PVG 04:55 · LAX 13:55 · JFK 16:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.