鉴于希望从其内容中抽象出循环缓冲区的结构,并从以下代码段开始(由此维基百科条目提供):
typedef struct
{
int value;
} ElemType;
typedef struct
{
int size; /* total number of elements */
int start; /* index of oldest element */
int count; /* index at which to write new element */
ElemType *elements; /* vector of elements */
} CircularBuffer;
void cbInit(CircularBuffer *cb, int size) {
cb->size = size;
cb->start = 0;
cb->count = 0;
cb->elements = (ElemType *)calloc(cb->size, sizeof(ElemType));
}
Run Code Online (Sandbox Code Playgroud)
如何抽象元素类型,以便在定义CircularBuffer的实例时指定它?我到目前为止的尝试如下:
CircularBuffer *cbInit(uint16 size, void *element)
{
CircularBuffer *buffer;
buffer …Run Code Online (Sandbox Code Playgroud) 可能重复:
取消引用void指针
我有这样一个函数调用:
void foo(void *context) //function prototype
..
..
..
main()
{
.
.
foo(&(ptr->block)); //where ptr->block is of type integer.
.
.
.
void foo(void *context)
{
Here I try to use the ptr->block but am having problems. I have tried
if((int *)context ==1)
..
..
}
Run Code Online (Sandbox Code Playgroud)
我在函数中将它转换回int以使用它.我在foo()函数中取消引用它是错误的吗?