使用 void 指针来实现通用堆栈?
我想知道这是否可能,但是您可以使用 void 指针来实现通用堆栈(或任何其他通用结构)。例如,我正在尝试下面的代码。 Push 和 Pop 的函数编译,但我不明白如何在 main 中处理它们。
我想知道这是否可能?当您像我在结构定义中那样创建一个 void 指针时,这并没有为数据分配任何实际存储,不是吗?例如,在推送例程中:
elem->data = data; //这不是创建任何新的存储,数据指向现有变量
在我看来,在C中执行类似操作的唯一方法是传递一个void指针并使用if语句来单独处理每个数据类型(数据类型有作为输入传递)?是这样吗?
typedef struct Element {
struct Element *next;
void *data;
} element;
typedef element * element_ptr;
typedef void * void_ptr;
typedef int * int_ptr;
void pop (element_ptr *stack, void_ptr *data)
{
element *elem;
elem = *stack;
*data = elem->data;
*stack = elem->next;
free(elem);
}
void push (element_ptr *stack, void *data)
{
element *elem;
elem = (element *) malloc(sizeof(element));
elem->data = data;
elem->next = *stack;
*stack = elem;
}
main()
{
element *stack;
void *data;
int num, num2;
int_ptr num_ptr;
num_ptr = &num2;
num = 5;
push(&stack,&num);
pop(&stack,&num_ptr); //This line gives a warning
printf("Popped %d from stack\n",num2); //Does not work.
}
I am wondering if this is even possible, but can you use void pointers to implement a generic stack (or any other generic structure). For example, I am trying out the code below code. The functions for push and pop compile, but I don't understand how to handle them in main.
I am wondering if this is even possible? When you create a void pointer as I do in the structure definition, that is not allocating any actual storage for data is it? For example, in the push routine:
elem->data = data; //This is not creating any new storage, data points to existing variable
It would seem to me that the only way to do something like this in C is to pass a void pointer and have if statements which separately handle each datatype (the datatype has to be passed as an input)? Is that right?
typedef struct Element {
struct Element *next;
void *data;
} element;
typedef element * element_ptr;
typedef void * void_ptr;
typedef int * int_ptr;
void pop (element_ptr *stack, void_ptr *data)
{
element *elem;
elem = *stack;
*data = elem->data;
*stack = elem->next;
free(elem);
}
void push (element_ptr *stack, void *data)
{
element *elem;
elem = (element *) malloc(sizeof(element));
elem->data = data;
elem->next = *stack;
*stack = elem;
}
main()
{
element *stack;
void *data;
int num, num2;
int_ptr num_ptr;
num_ptr = &num2;
num = 5;
push(&stack,&num);
pop(&stack,&num_ptr); //This line gives a warning
printf("Popped %d from stack\n",num2); //Does not work.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
void *
可以用作任何指针类型的占位符。我不完全确定你在问什么,但如果你想要一个int
的堆栈,那么你可以创建你的对象(在堆上),然后将指向int的指针传递给push方法- 但您必须将指针强制转换为void
:void * void_ptr =
(void*)pointer_to_int
棘手的部分是当您将指针强制转换回
int
时pop - 这需要知道堆栈保存的是int
而不是float
等。希望有所帮助。
First off, a
void *
can be used as a place holder for any pointer type. I'm not entirely sure what you are asking, but if you want a stack ofint
, then you can create your object (on the heap), then pass a pointer to the int to the push method - but you must cast the pointer tovoid
:void * void_ptr =
(void*)pointer_to_int
The tricky part is casting back to
int
when you pop - this requires knowing that the stack holdsint
and notfloat
etc.Hope that helps.