c - 理解 C 结构

标签 c pointers struct function-pointers forward-declaration

我试图了解以下 C 结构中发生的事情:

/* EXCERPT from LINES 59-90 */
/* parse.h
 * Copyright (c) 2011, Peter Ohler
 * All rights reserved.
 */

typedef struct _ParseInfo {
    // used for the string parser
    const char      *json;
    const char      *cur;
    const char      *end;
    // used for the stream parser
    struct _Reader  rd;

    struct _Err     err;
    struct _Options options;
    VALUE       handler;
    struct _ValStack    stack;
    CircArray       circ_array;
    int         expect_value;
    VALUE       proc;
    VALUE       (*start_hash)(struct _ParseInfo *pi);
    void        (*end_hash)(struct _ParseInfo *pi);
    VALUE       (*hash_key)(struct _ParseInfo *pi, const char *key, size_t klen);
    void        (*hash_set_cstr)(struct _ParseInfo *pi, Val kval, const char *str, size_t len, const char *orig);
    void        (*hash_set_num)(struct _ParseInfo *pi, Val kval, NumInfo ni);
    void        (*hash_set_value)(struct _ParseInfo *pi, Val kval, VALUE value);

    VALUE       (*start_array)(struct _ParseInfo *pi);
    void        (*end_array)(struct _ParseInfo *pi);
    void        (*array_append_cstr)(struct _ParseInfo *pi, const char     *str, size_t len, const char *orig);
    void        (*array_append_num)(struct _ParseInfo *pi, NumInfo ni);
    void        (*array_append_value)(struct _ParseInfo *pi, VALUE value);

    void        (*add_cstr)(struct _ParseInfo *pi, const char *str, size_t len, const char *orig);
    void        (*add_num)(struct _ParseInfo *pi, NumInfo ni);
    void        (*add_value)(struct _ParseInfo *pi, VALUE val);
} *ParseInfo;

来自 https://github.com/ohler55/oj/blob/master/ext/oj/parse.h#L59 :

我不明白第 74 行(上面 list 中的第 22 行)发生了什么。

开始于:

    VALUE       (*start_hash)(struct _ParseInfo *pi);

谁能解释一下这些线在做什么?

最佳答案

它们是 function pointers语法是:return-type (*pointer_name)(args)

但是,开发人员使用了一种特殊技术,由第一个参数表示,它始终是指向 _Parser 结构的指针。它确实允许一种简单形式的面向对象编程,其中类可以具有专门作用于必须手动设置和处理的特定对象实例的方法。 @paxdiablo 的出色回答中的更多信息 here .

注意:存在多项 ISO C 违规行为,因为您不允许在名称前加上 _ 和大写名称。更多信息 here .

关于c - 理解 C 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30355701/

相关文章:

c - 使用类型转换或使用尾随数据类型标识符。 (uint16_t)10 还是 10u?

C:使用带有函数指针的二维数组

c - 函数说节点已删除但并未删除

struct - 在 View Controller 之间快速传递结构?

c++ - 为什么结构体的 sizeof 不等于每个成员的 sizeof 之和?

c - 结构声明和指针在一行中

c - Arduino Uno Timer1 似乎自行启动

c - 如何在 Tizen Gear S2 本地文件系统上写入文本文件?

c - 一些指针问题

c - 当我尝试声明指针并通过 malloc 获取它时,gcc 期望表达式