2

I'm trying to create my stack, using my linked listed functions that I already created. I have the following:

listNode *createList()
{
    listNode *anyNode;
    anyNode = malloc(sizeof(listNode));
    anyNode->next = NULL;
    return anyNode;
}

stack *createStack()
{
    stack *temp;
    stack.list = createList();
    return temp;
}

and in my main I call it using:

int main(void)
{   
    stack *theStack;
    theStack = createStack();
    return(0);
}

I defined my stack as:

typedef struct stack {
    listNode *list;
}stack;

I get a compiler error: expected identifier or '(' at my stack.list = createList() call in my createStack function. I'm not sure why.

My main works if I change

stack *theStack 

into

 listNode *list 

then just call it as: list = createList();

13
  • 2
    return(0) looks as if return was a function, it's not. Commented May 31, 2016 at 1:15
  • @iharob and what's your point??? it's perfectly valid, take a look at the BSD source if you aren't happy about it. Commented May 31, 2016 at 1:16
  • @self there is no point, just style advice. Commented May 31, 2016 at 1:16
  • Do you have any questions about the actual implementation (the interesting part)? Commented May 31, 2016 at 1:17
  • 1
    @self I am not acting like that, I am just trying to point out that it's deprecated style that and that it's not needed. Read my comment again please. Commented May 31, 2016 at 1:21

2 Answers 2

1

You are declared temp as a pointer of type stack but then you write

stack.list = ...

stack in your code is a type not a variable, you can't use it like that.

You need a variable instead, something like

stack my_stack;
my_stack.list = ...

if you want it to be a pointer, then you need to use malloc() to allocate space for it.

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

4 Comments

I think that worked. I have a question for printing now. I wrote my push function and I think it works as well, how do I test printing the nodes in my stack go? Im not sure how it works since my stack only takes a listNode* list.
In my linked list, I would just print printf("%d",list->value);
@iharob when you return a pointer to this my_stack variable, it will be returning a local variable. Better to declare a pointer and call the createstack function on it.
@RishikeshRaje I never suggested to do that. If the stack variable will only be used locally there is no harm in doing it without a pointer, although mostly that would not be the case. My answer tries to illustrate the syntax error rather than how it should be done.
0

The problem is in the createstack function.

stack.list = createList()

stack is a global variable, and you should be using the local variable temp here. You need to allocate size for the stack structure also here.

stack *createStack()
{
  stack *temp;
  temp = malloc(sizeof(stack));
  temp->list = createList();
  return temp;
}

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.