0
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!

2
  • 1
    You are in the most serious need of reading a beginner C book or tutorial. Commented Dec 6, 2013 at 11:49
  • Your text on how to write a function and declare its parameters surely has an example of how to invoke that function. Commented Dec 6, 2013 at 11:49

2 Answers 2

1
  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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also, function create_stack() should declare its return type: STACK create_stack() or else it defaults to int.
0

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.

Comments

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.