相关疑难解决方法(0)

如何在C中实现函数重载?

有没有办法在C中实现函数重载?我正在寻找简单的函数来重载像

foo (int a)  
foo (char b)  
foo (float c , int d)
Run Code Online (Sandbox Code Playgroud)

我认为没有直接的方式; 我正在寻找解决方法,如果存在的话.

c overloading

234
推荐指数
12
解决办法
18万
查看次数

在C中模拟模板(用于队列数据类型)

我正在尝试queue使用C 实现一个结构.我的实现非常简单; 队列只能保存ints而不能保留其他内容.我想知道我是否可以模拟C++模板C(可能通过使用预处理器#define),以便我queue可以保存任何数据类型.

注意:我不想用void*.我认为它有点风险,很容易导致奇怪的运行时错误.

c templates

58
推荐指数
4
解决办法
5万
查看次数

通过数组在C中排队

我用C语言实现了一个使用结构数组的队列.

typedef struct{
    req_t     buffer[BUFFER_SIZE];   // buffer
    uint16_t    size;                          // length of the queue
    uint16_t    count;                 // number of elements present in the queue
    req_t     *p_head;                 // pointer to head of the queue (read end)
    req_t     *p_tail;                 // pointer to tail of the queue (write end)
}circular_buffer_t;

void init_cb(circular_buffer_t *p_cb){

    p_cb->p_head = p_cb->buffer;
    p_cb->p_tail = p_cb->buffer;
    p_cb->count = 0;
    p_cb->size = BUFFER_SIZE;

}
Run Code Online (Sandbox Code Playgroud)

问题是上面给出的实现仅可用于存储req_t结构的实例.现在我需要存储另一个结构的实例,我不知道如何以更一般的方式定义队列,以便我能够为不同结构的实例使用相同的队列.问题是我需要在缓冲区定义之前知道结构类型.有人知道如何解决这个问题吗?

    #ifndef CIRCULAR_BUFFER_H_
#define CIRCULAR_BUFFER_H_

#define BUFFER_SIZE 32

// macro creates variant of the queue for each …
Run Code Online (Sandbox Code Playgroud)

c queue

4
推荐指数
1
解决办法
624
查看次数

标签 统计

c ×3

overloading ×1

queue ×1

templates ×1