-1
Stack create(int c)
{
    Stack S=(Stack)malloc(sizeof(struct stack));

    S->size=c;
    S->top=-1;
    S->array=(char *)malloc(sizeof(char)*c);

    return S;
}

Stack makeEmpty(void)
{
    Stack *S1=create(100);
    S1[0]->top=-1;
    return S1;
}

char pop(Stack S)
{
    return S->array[S->top--];
};

int main(void)
{
    Stack *S1;
    S1=makeEmpty();
    int j;
    int k=0;
    char result[30];
    for(j=0; j<2; j++)
    {
        char result1=pop(S1);
        strcat(result, result1);
        k++;
    }
}

I skipped some parts, like typedef struct stack Stack; What I wanted to do was pop out elements from the stack while for-loop works. Then, store those elements in a new array which is result. To check whether it works or not, I printed out but I had a runtime error. How to store the element and how to print it out?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
stella
  • 25
  • 1
  • 8
  • Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, but concentrate on how to create an MCVE ([MCVE]) more urgently. "I had a runtime error" is an appalling explanation of what went wrong. What really happened, and what had you input? The code you show doesn't define the structure type, nor show how data is pushed. Most often, you should predecrement the stack pointer, but it depends... And the problem is as likely to be in the code you haven't shown as in the code you do show. – Jonathan Leffler Jan 03 '18 at 01:16
  • I don't understand what you are asking. Besides, the signature of `strcat` is `char *strcat(char *dest, const char *src);`. You are passing a `char`, your compiler must have warned you about it. – Pablo Jan 03 '18 at 01:16
  • How does your `pop()` function tell you there wasn't a character to pop? You haven't shown how things get pushed, but I do see that you have `-1` in the `top` for an empty stack, so the `S->top--` isn't necessarily wrong. – Jonathan Leffler Jan 03 '18 at 01:18
  • In `makeEmpty` you have a syntax error, `S1[0]->top=-1;` is wrong, it should be `S1[0].top=-1;`. Do you compile your stuff before posting it here? Do you read the compiler error? Besides, doing this is pointless, the `create` call already assigns `-1` to `top`. – Pablo Jan 03 '18 at 01:22
  • I tried both one, but S1[0].top=-1 made a compile error. – stella Jan 03 '18 at 01:24
  • you requested for memory but didn't release it. And [don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Jan 03 '18 at 07:33

1 Answers1

0

I've made copy&paste of your code, and it doesn't get compiled. I think that you are either not posting your actually code nor you don't bother to compile and read the compiler warnings. It's rather difficult to help you. Here some things I noticed:

1. create must return a pointer to Stack, not the object.

Stack *create(int c)
{
    Stack *S = malloc(sizeof *S);

    S->size=c;
    S->top=-1;
    S->array = malloc(c);

    return S;
}

2. Same goes for makeEmpty

Stack *makeEmpty(void)
{
    Stack *S1=create(100);
    S1->top=-1;
    return S1;
}

3. pop should get a pointer to Stack, not the object

char pop(Stack *S)
{
    return S->array[S->top--];
};

Here you should check whether there are elements on your stack. int pop(Stack *S, char *val) where it returns 1 and writes on *val on success, and returns 0 otherwise would be better.

4. Judging from your pop you are pushing char only. I don't get what you are trying to do with strcat. Either way, you are doing strcat wrong. You are declaring a stack with 100 spaces, but you are only declaring 30 spaces for result. What if you have more than 31 elements on your stack? I know that you are only inspecting 2 elements but it's easy to overlook that and expand it to go through all the stack without changing the memory requirements for result.

Also strcat is a function that works with C-Strings, that means it expects C-Strings. A C-String must be \0 terminated, yours are not. You have something that looks like a C-String but it's not. If you insist on using strcat, the you should do it like this:

for(j=0; j<2; j++)
{
    char result1[] = { pop(S1), 0 };
    strcat(result, result1);
}
Pablo
  • 13,271
  • 4
  • 39
  • 59