Alex K
Alex K

Reputation: 11165

Why do C programs keep saying 'struct' every time they refer to one?

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?

Upvotes: 7

Views: 2596

Answers (5)

Soren
Soren

Reputation: 14688

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.

Upvotes: 3

Many modern C libraries are systematically typedef-ing their struct and use some naming convention. For example gtk/gtkalignment.h has:

typedef struct _GtkAlignment              GtkAlignment;
typedef struct _GtkAlignmentPrivate       GtkAlignmentPrivate;
typedef struct _GtkAlignmentClass         GtkAlignmentClass;

My own habit is to name foo_st the struct and foo_t the type, so I often code

typedef struct foo_st foo_t;

And I would use union foo_un if it where a union.

Upvotes: 1

Erwin Smout
Erwin Smout

Reputation: 18408

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 ?

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

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.

Upvotes: 7

Mat
Mat

Reputation: 206689

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:

  • use struct thing as the type specifier
  • make a typedef like typedef struct thing thing_t; and use thing_t as the type specifier.

(C++ is different in this respect.)

Upvotes: 8

Related Questions