使用 void 指针来实现通用堆栈?

发布于 2024-10-27 09:26:20 字数 1055 浏览 1 评论 0原文

我想知道这是否可能,但是您可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我 2024-11-03 09:26:20

首先,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 of int, 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 to void:

void * void_ptr = (void*)pointer_to_int

The tricky part is casting back to int when you pop - this requires knowing that the stack holds int and not float etc.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文