Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

Purpose of Name before definition in C typedef struct

typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;

I have come across the above example, where I can only define objects using Book and using Books gives unknown type. I understand that I can do without Books altogether, then what is the purpose of having it in the first place?

Upvotes: 2

Views: 186

Answers (4)

ryyker
ryyker

Reputation: 23208

using Books gives unknown type

This is good. It suggests your compiler is one of recent design, following current trends in C programming. As you have discovered, using the name Books is superfluous, and even generates warnings / errors. (compiler dependent)

Using the following syntax allows you to simply use Book to declare a new instance of the struct Book, (without having name Books) :

typedef struct
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;  

Book book, *pBook; //etc.  

And, it also tends to make code a little less verbose, i.e. having to write struct Book instead of just Book everytime we want to use it declaring a new instance, or using it in a function argument:

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

The only type declared by your code is struct Books. A typedef never declares a type, but only creates a new name for another type. E.g

typedef int int0;
typedef int int1;

Creates two new names for int that are interchangeable with int, no new integer type is invented.

For type names that consist of several tokens, typedef is a convenient tool to abbreviate it to a one-token name

typedef unsinged long long int ullong;
typedef struct toto Toto; 

The later one for struct toto has even the particularity to forward declare the struct, so you can then do

struct toto {
  Toto* next;
};

that is refer to struct toto inside its own definition.

The tag name space is distinct from the identifier namespace, so something as the following is even valid

typedef struct toto toto;
struct toto {
  toto* next;
};

Upvotes: 0

Aaron McDaid
Aaron McDaid

Reputation: 27133

You can leave out the first Book, like this:

typedef struct
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;

But then that wouldn't work for linked lists, where the struct needs to be able to hold a pointer to itself:

struct List
{
    int data;
    struct List * next;  //  this line requires that List appear with 'struct'
};

The line struct List *next; needs to know there is a struct called List - this requires that struct List appear before that line.

If you leave out the second, then you would need to type struct Book instead of Book every time you wanted to use it in the rest of your program. (Some consider that a feature, not a bug!)

(In C++, for reference, the first Book is sufficient and it is not necessary to use struct elsewhere and therefore the typedef is not useful.)

Upvotes: 1

clcto
clcto

Reputation: 9648

You can use struct Books as a type. It is shorthand for

struct Book_t
{
   //...
};

typedef struct Book_t Book;

Upvotes: 0

Related Questions