1

I'm working on a little project in pure C. I use gcc compiler (and thats the idea). I have a struct as follows:

struct STACK{ char var; struct STACK* next; struct STACK* prev; }; 

and little lower in my function f():

STACK head; head->var='a'; printf("%c", head->var); 

Now, during compilation I get the error:

: In function ‘f’: :13:3: error: unknown type name ‘STACK’ :14:7: error: invalid type argument of ‘->’ (have ‘int’) :15:20: error: invalid type argument of ‘->’ (have ‘int’) 

Could someone explain me what is wrong ? When I was working in C++ and g++ everything seemed fine.

================= EDIT:

OK, that worked, but now I have to dynamically create structures of type STRUCT and only keep pionters to them. Is it possible in C ?

0

3 Answers 3

4

You need to add the struct keyword before the struct name STACK:

struct STACK head; 

instead of:

STACK head; 

Also you are using -> which is usually applied to pointers. Just use the . instead:

head.var 

Remember that:

x->y 

can be translated to:

(*x).y 

Finally if you define a struct as:

typedef struct { char var; struct STACK* next; struct STACK* prev; } STACK; 

you can declare the variable as:

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

Comments

1

The definition is okay, but to declare a variable, try:

struct STACK head; 

And to refer to it's members,

head.var = 'a'; 

2 Comments

Thank you very much, that worked. Can you please tell me why arrow notation does not ?
The arrow is used when head contains a pointer to a structure. When the variable is the struct itself, we refer to the members with dot.
1

As you have not provided a typedef for your struct you need to declare as

struct STACK head; 

also head is not a pointer so the use of -> should be replaced with . when you accees its members

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.