location of names and typedef

B

Baloff

Hello

what is the struct name and what is the typedef identifier?

example 1:
struct firstname {
....code
};
is the firstname here the struct and type name?
----------------------------------------------------------------------
example 2:
struct {
....code
} lastname;
does this exist in the language, is so what's it mean?
----------------------------------------------------------------------
example 3:
struct firstname {
....code
}lastname;
does this exist in the language, is so what's it mean?
----------------------------------------------------------------------
example 4:
typedef struct {
....code
} lastname;
is lastname the type, and why the firstname missing?
----------------------------------------------------------------------
example 5:
typedef struct firstname{
....code
} lastname;
why 2 names?
----------------------------------------------------------------------
why all the above variations and when most applied?

does this go on enum and union alike?


thank you
 
V

Victor Bazarov

Baloff said:
what is the struct name and what is the typedef identifier?

example 1:
struct firstname {
...code
};
is the firstname here the struct and type name?

Well, yes, essentially. We use the term "type-id" more, but you can
call it "struct name" or "type name".
----------------------------------------------------------------------
example 2:
struct {
...code
} lastname;
does this exist in the language, is so what's it mean?

'lastname' is an object of unnamed type.
----------------------------------------------------------------------
example 3:
struct firstname {
...code
}lastname;
does this exist in the language, is so what's it mean?

'lastname' is an object of type 'firstname'. 'firstname' is a type-id.
----------------------------------------------------------------------
example 4:
typedef struct {
...code
} lastname;
is lastname the type, and why the firstname missing?

'lastname' is a _synonym_ of the unnamed type. It can be used almost
anywhere a type-id is expected. At this time I don't recall where it
cannot be used.
----------------------------------------------------------------------
example 5:
typedef struct firstname{
...code
} lastname;
why 2 names?

This is a usual C approach. 'firstname' is a type-id. 'lastname' here
is a 'typedef-id', the synonym for 'firstname'. In C you can't use
'firstname' alone, it won't work. You need to say "struct firstname",
but with a typedef you don't have to supply 'struct', and you can just
write "lastname". It will mean the same.

Some of those variations are really rarely used. Why? I am not sure.
Probably because they are quite specific and there is no need in them
in most cases. I'd say that variation 1 is the most used.
does this go on enum and union alike?

Yes, IIRC.
 
V

Victor Bazarov

Baloff said:
what about:
typedef struct firstname{
...code
};

What about it? I think it's a syntax error. You're supposed to
have the typedef-id after the type...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top