Though I rarely write C code, I often see it (mostly due to books in my field having it as sort of reference language for algorithm examples) and something has been bugging me for a while about the way variables/parameters are declared. The best example would be this List with a twist example, about a particular implementation of Linked List used in Linux kernel (sorry, I had a link to a blog post originally, but apparently the blog post has been deleted, I copied code from my browser's cache).
struct blog {
...
struct ls posts;
};
struct post {
...
struct ls blog_posts;
};
void delete_blog(struct blog *blog) {
...
struct post *p;
ls_for_each(&blog->posts, p, blog_posts) {
free(p);
}
free(blog);
}
What bugs me is the fact that they keep repeating the keyword struct everywhere. I mean things like ls, blog, post are declared as structs, so what is the point of saying it is a struct every time you declare a variable or a function parameter of that type? What does this tell the compiler that it can't infer from the fact that the thing you are instantiated has been defined as a struct?
In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name. How to define structures? Before you can create structure variables, you need to define its data type.
To prevent these problems, it's a good idea to typedef your classes and structs in C++, too, even though at first glance it appears to be unnecessary. In C++, with the typedef the name hiding become an error that the compiler tells you about rather than a hidden source of potential problems.
If you are coming from a C++ background, You are used to using structures without the struct keyword appearing before it everywhere.But in C the language syntax needs the keyword to be pre-pended everywhere.It is indeed a pain but that is how the language syntax was designed. see stackoverflow.com/questions/1675351/…
It's interesting that the example given here (in which the typedef prevents using "struct" "all over the place") is actually longer than the same code without the typedef, since it saves exactly one use of the word "struct". The smidgen of abstraction gained rarely compares favorable with the additional obfuscation.
Well, it's because if there isn't a typedef, that's the only way to refer to the struct.
In C, if you have:
struct thing {
// stuff here
};
and you want to have a variable of that type, you need to either:
struct thing
as the type specifiertypedef struct thing thing_t;
and use thing_t
as the type specifier.(C++ is different in this respect.)
struct
tags are in a different namespace than usual identifiers. So the struct
keyword is necessary to specify that. A famous example is in POSIX where you have a struct stat
and a function stat
.
It is just the way the syntax is...
C is a quite old language, and it purposely created a syntax which differentiated between the typed which could be represented directly by machine instructions and those which were more complex and spanned several memory locations.
"Typedef" was added to the language to allow the creation of shorter names which didn't included the struct, however the full struct syntax is used - and specifically is used in historical context where the original definition of a system structure is defined as "struct" without a corresponding typedef short form.
As comparison C++ which is a much later language, allows a class/struct to be referred to by just the class/struct name regardless of whether it was defined with a typedef.
So as questions goes, the answer is that it just is like that because it is...
EDIT: Just looking at your example code, there is one notable case where struct adds something to the compiler behavior -- in most cases a type has to be defined before it can be used in a another declaration -- except for when you refer to a struct sometype*
(i.e. a pointer to a struct) in which case the compiler is happy to have the sometype
defined after the use.
so
struct post {
struct post *nextpost;
struct post *prevpost;
...
};
becomes possible, as post
is not defined until the closing bracket of the struct.
struct is part of the type declaration.
If a function takes an argument (/parameter), then the type of that argument must be declared. How could you declare that that argument is a struct if you cannot use the word struct ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With