Interview Question: Different ways of structure declaration in C !

This happens to be a most popular question if you are attending an interview for an embedded system company.

Question: What is the difference between

a.

struct Embedded
{

int a;

};

b.

typedef struct

{

int a

} Embedded;

Answer: 

Basically, structures are a mechanism by which we can create our own data type to represent the real world objects/entities.

In the first case, structure has a structure tag ‘Embedded’. The way we create objects out of these structures are using the keyword ‘struct’. Example:

struct Embedded e ; // here ‘e’ is an object of type ‘struct Embedded’

In the second case, structure does not have a structure tag. Rather, it is a typedef. Sometimes, programmers does not even know that, they are using a structure. In other words, it is more of an ‘abstract type‘. To create objects out of these structures, we need not use the ‘struct’ keyword. Example:

Embedded e; // here  ‘e’ is an object of type ‘struct Embedded’.

If you have been working with files in C, you would know the pointer to a file is of type

FILE *

where, FILE is something that looks like below:

typedef struct {
        unsigned char   *_ptr;
        int     _cnt;
        unsigned char   *_base;
        unsigned char   *_bufendp;
        short   _flag;
        short   _file;
        int     __stdioid;
        char    *__newbase;
        long     _unused[1];
} FILE;

Author Information:

Subhash.K.U, Principal Mentor, Subhash Programming Classes,Bangalore.

VIEW SUBHASH’s PROFILE

SUBHASH LOVES TO TEACH ! CLICK HERE !

One Response Comment

  • Chandra prashad MISHRA  July 11, 2016 at 12:20 pm

    I required more such kind of questions. It is really helpful for me.

    Reply

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.