Question

I have a stack that will hold a BIGINT. A bigint is a struct that has a char array that will hold our numbers, and some other values. So I made my stack, and everything worked well from the STDIN, but when I try to add a a bigint, it seems like nothing is being added. here is my push_stack() code:

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

Here is my stack structure:

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

Here is my bigint structure:

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;
}

And now my additon to the stack:

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

For some reason, my bigint will not add to the stack. Any help is appreciated.

Was it helpful?

Solution

You call push_stack() passing a pointer to a bigint.

push_stack() is expecting a stack_item (not a pointer to a bigint).

You then strdup() the item (which is not a char* to a 0 terminated string as expected by strdup).

You should have received compiler warnings when you built this. Try fixing those first!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top