堆栈抽象数据类型C
我正在尝试为C中的堆栈构建和抽象LIB,但是由于可变void *数据,我在定义推动功能时遇到了一些困难。如何解决推动问题?
是否有更好的方法来创建具有相同目的的抽象LIB?
typedef struct
{
Data_Type data_type;
size_t size;
int top;
void *data;
} Stack;
void push(Stack *stack, void *n)
{
void *ptr = stack->data;
*(ptr + stack->size * stack->top) = *n;
}
I'm trying to build and abstract lib for a stack in c, but i'm having some trouble defining the push function because of the variable void *data. How can i solve the push problem?
Is there a better way of creating an abstract lib with the same purpose?
typedef struct
{
Data_Type data_type;
size_t size;
int top;
void *data;
} Stack;
void push(Stack *stack, void *n)
{
void *ptr = stack->data;
*(ptr + stack->size * stack->top) = *n;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能删除void Pointer 。在您的
按下
函数的第二行上,您在分配的左侧和右侧都取消了一个空隙指针。我建议您删除该行,而是使用memcpy
将正确的字节从n
复制到堆栈中。You cannot dereference a void pointer. On the second line of your
push
function, you are derefencing a void pointer on both the left and right sides of the assignment. I suggest you delete that line and instead usememcpy
to copy the right number of bytes fromn
into your stack.