3

I know the basic difference between them just have a doubt in particular situation like following:

struct books{
    int id;
    char* title;    
  }book;

book.id=9;    // this is valid;

But in case of typedef :

typedef struct books{
  int id;
  char*title;
}book;
book.id=9;    //it is not valid we have to do like book b1; then b1.id=9 is valid

What is going on here can u tell me?

1
  • 1
    Well, the first example defined a variable book of type struct books. The second defines a type alias book to the type struct books, but no variable. Saying book.id = 9 here is like saying float = 1.2f. Commented Mar 17, 2016 at 10:26

1 Answer 1

6

In your first case, you are creating an object of type struct books named book.

In the second, you are defining an alias book for the type struct books. Thus book is not an object but a type name.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.