在结构中删除无效**

发布于 2025-02-02 15:51:35 字数 1233 浏览 3 评论 0原文

因此,我正在尝试使用打击来实现堆栈,并且我正在腌制...

我的.c文件定义:

typedef struct stack
 {
  unsigned int capacity;
  unsigned int size;
  unsigned int element_size;
  void **buffer;
}stack_t;

stack_t *Create(const unsigned int capacity, const unsigned int element_size)
{ 
  stack_t *ptr;
  ptr = (stack_t *)malloc(sizeof(stack_t));
  ptr->capacity = capacity;
  ptr->element_size = element_size;
  ptr->size = -1;
  ptr->buffer = malloc(sizeof(void *) * capacity);
  return ptr;
}
int Push(stack_t * stack, void * const ptr)
{ // void * const ptr - hold the char we want to pass into the stack
  assert(stack);
  if ((stack->size + 1) < stack->capacity)
  {
    stack->size++;
    *(stack->buffer + stack->size) = ptr;
    printf("Test: %c\n", *(char*)*(stack->buffer + stack->size));
    return (0);
  }

  else
  {
    printf("Stack Overflow\n");
  }
}

我的问题是我不知道用户将输入哪种类型的变量,用于测试我每制造一系列字符,以查看它是否有效和一系列int,并且可以很好地工作。

如果我尝试在给定的位置将我的空白**缓冲区延迟,并使用此格式打印值,则可以使用:

 *(type-cast)*(buffer->stack + buffer->size)

如果未指定类型定义,该怎么办? 大概是用户输入的,我还分配了一个element_size成员,以便用户可以输入每个元素所包含的字节数量,但似乎无法弄清楚如何在不知道的情况下真正退出参考(类型铸造)事先。

如果您需要我发布整个代码,请告诉我。

So I'm trying to implement a stack using a struck and I'm in a pickle...

My .C File definitions:

typedef struct stack
 {
  unsigned int capacity;
  unsigned int size;
  unsigned int element_size;
  void **buffer;
}stack_t;

stack_t *Create(const unsigned int capacity, const unsigned int element_size)
{ 
  stack_t *ptr;
  ptr = (stack_t *)malloc(sizeof(stack_t));
  ptr->capacity = capacity;
  ptr->element_size = element_size;
  ptr->size = -1;
  ptr->buffer = malloc(sizeof(void *) * capacity);
  return ptr;
}
int Push(stack_t * stack, void * const ptr)
{ // void * const ptr - hold the char we want to pass into the stack
  assert(stack);
  if ((stack->size + 1) < stack->capacity)
  {
    stack->size++;
    *(stack->buffer + stack->size) = ptr;
    printf("Test: %c\n", *(char*)*(stack->buffer + stack->size));
    return (0);
  }

  else
  {
    printf("Stack Overflow\n");
  }
}

My issue is that I don't know which type of variable the user will input, for my testing I've per-made an array of chars to see if it works and array of int and it works perfectly.

If I try to de-reference my VOID **buffer in a given location and print the value using this format it works:

 *(type-cast)*(buffer->stack + buffer->size)

What if the type definition isn't specified?
And is presumably entered by the user, I've also allocated an element_size member in my struck so the user can input the amount of BYTES each element holds, but can't seem to figure out how to really de-reference without knowing the (type-cast) beforehand.

If you need me to post the entire code let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文