我有一个可以容纳 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/