我怎样才能对 C 函数结构进行此更改?

How can i make this change on C function struct?

我一直在尝试在 C 上模拟 Python 实例构造,但我无法找到一种方法将结构名称返回给函数并将其作为参数

typedef struct queue {
    .
    .
    ssize_t (*add)(struct queue *, int);
} queue;

ssize_t add(queue *self, int value)
{
    /* Add a element to the queue */
}

int main(void)
{
    queue *clients = createQueue(5); /* Here i create the queue struct */

    clients.add(5); /* I want to convert this instruction to "clients.add(&clients, 5);" */
}

我试过使用宏,但我发现我不能使用正则表达式或类似的东西

很难 'emulate' 像 C 这样的非对象语言中的对象语言,但在 C++ 中很容易做到,例如 :

struct queue { // a 'struct' is a 'class' where all is 'public' by default
    .
    .
    ssize_t add(int); // in C++ 'this' corresponding to 'self' does not have to be explicitly placed in the params list
};

ssize_t queue::add(int value)
{
    /* Add a element to the queue */
    return ...a ssize_t...;
}

int main(void)
{
    queue * clients = createQueue(5); /* Here i create the queue struct */

    clients->add(5); // '->' rather than '.' because clients is a 'queue*' rather than a 'queue'
}

Python 'self' 的对应在 C++ 中命名为 'this' 并且您不必将其显式放置在参数列表中,它是隐式的。

注意在 C++ 中没有像 Python 中那样的垃圾收集器,可能 createQueue 在堆中分配队列,在这种情况下如果你想在结束之前释放分配的内存执行你必须做的 delete queue;

如果您自己不 return

main 末尾有一个隐含的 return 0;