user3074177
user3074177

Reputation: 9

error expected expression before "STACK"

typedef struct student *std_ptr;

   struct student
    {
        int number;
        std_ptr next;
    };
    typedef std_ptr STACK;

    create_stack(void)
    {
        STACK S;
        S = (STACK) malloc( sizeof( struct student ) );

        if(S == NULL) printf("out of space!");
        return S;
    }

    void push(int x, STACK S)
    {
        std_ptr tmp;
        tmp = (std_ptr) malloc(sizeof(struct student));

        if(tmp == NULL) printf("out of space!");

        else
        {
            tmp -> number = x;
            tmp -> next = S -> next;
            S -> next = tmp;
        }
    }

    int main()
    {
        push(12058010,STACK S);
        return 0;
    }

Im trying to call function and I get error: expected expression before stack.I also tried to call the function like that

    int main()
    {
        push(12058010,S);
        return 0;
    }

This time I get error: 'S' undeclared(first use in this function)

Thank you for your help!

Upvotes: 0

Views: 555

Answers (2)

alk
alk

Reputation: 70911

  1. Define the variable s by doing:

    STACK s;
    
  2. Initialise it:

    s = create_stack();
    
  3. Test whether the initialisation succeeded:

    if (NULL == s)
    {
      return EXIT_FAILURE;
    }
    
  4. Use it by calling push() like this:

    push(12058010, s);
    

All together this could look like this:

int main(void)
{   
    STACK s = create_stack(); /* This merges step 1 and 2. */
    if (NULL == s)
    {
      return EXIT_FAILURE;
    }
    push(12058010, s);
    return EXIT_SUCCES;
}

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234665

S is neither in the global scope nor in the scope of main().

I suspect you meant to write STACK S = create_stack(); as the first statement in main().

Don't forget to free the allocated memory as well.

Upvotes: 0

Related Questions