c - 添加指向堆栈的指针 C

标签 c pointers stack

我有一个可以容纳 BIGINT 的堆栈。 bigint 是一个结构,它有一个 char 数组,可以保存我们的数字和一些其他值。所以我创建了堆栈,STDIN 中的一切都运行良好,但是当我尝试添加 bigint 时,似乎没有添加任何内容。这是我的 push_stack() 代码:

void push_stack (stack *this, stack_item item) {
    if (full_stack (this)){realloc_stack (this);}
    this->data[this->size] = strdup(item);
    this->size++;
}

这是我的堆栈结构:

 struct stack {
   size_t capacity;
   size_t size;
   stack_item *data;
};

这是我的 bigint 结构:

struct bigint {
    size_t capacity;
    size_t size;
    bool negative;
    char *digits;
};

bigint *new_string_bigint (char *string) {
    assert (string != NULL);
    size_t length = strlen (string);
    bigint *this = new_bigint (length > MIN_CAPACITY ? length : MIN_CAPACITY);
    char *strdigit = &string[length - 1];
    if (*string == '_') {
       this->negative = true;
       ++string;
    }
    char *thisdigit = this->digits;
    while (strdigit >= string) {
        assert (isdigit (*strdigit));
        *thisdigit++ = *strdigit-- - '0';
    }
    this->size = thisdigit - this->digits;
    trim_zeros (this);
    return this;
}

现在我对堆栈的补充:

void do_push (stack *stack, char *numstr) {
    bigint *bigint = new_string_bigint (numstr);
    show_bigint(bigint);
    push_stack(stack, bigint);
}

出于某种原因,我的 bigint 不会添加到堆栈中。感谢您的帮助。

最佳答案

您调用 push_stack() 传递一个指向 bigint 的指针。

push_stack() 需要一个 stack_item(不是指向 bigint 的指针)。

然后 strdup() 项目(这不是 strdup 预期的以 0 结尾的字符串的 char*)。

当您构建它时,您应该已经收到编译器警告。先尝试解决这些问题!

关于c - 添加指向堆栈的指针 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159307/

相关文章:

充当 API 的 C 程序

c++ - 变量如何在自己的声明中获取自身的地址?

c - 返回分配指针

c# - C#中的堆栈容量

c++ - 栈函数的实现在哪里?

c - C 编程语言(第二版): Exercise 1-15 (Celsius to Fahrenheit conversion) - What is wrong with my solution?

c - 我可以将什么传递给 fopen?

python - 如何将数组和结构传递给需要 3 个 u32 指针作为参数的 c 函数(ctypes)

c++ - 将 int** 转换为 "pointer to a two-dimensional array of integers with fixed number of elements per column"

typedef 结构体中 malloc/realloc 的正确用法