1

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.

5
  • Is it because I am using strdup? Commented Mar 3, 2014 at 22:57
  • You have not included stack definition Commented Mar 3, 2014 at 22:58
  • 1
    what is stack_item? Unless it's a char* to a 0 terminated string you can't strdup() it. Commented Mar 3, 2014 at 23:05
  • What does your realloc_stack function look like? Commented Mar 3, 2014 at 23:07
  • Why don't you just assign the pointer directly? Are you trying to use strdup to create a copy of the bigint (which won't work, since a bigint isn't a string)? Otherwise, why not just do this->data[this->size] = item? Commented Mar 3, 2014 at 23:15

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

stack_item is a a generic data type thou
A generic data type in C? How have you defined it? void*?
What he's saying is: You can't use strdup on anything other than a char * (or something that points to a null-terminated string atleast), but since you're trying to do that you should have received compiler warnings (and non-working code ofcourse).
what should I use instead of strdup?
should i have the stack point to bigints->digits ? that is a char*
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.