Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do structs that have been typedef-ed work in C?

Structures with tags allow you to make a instances (or whatever an instance is called in c). What about typedef structs though? If the typedef is for the struct keyword to be replaced by a type name, then every time you use the the typename aren’t you just saying that you are replacing the struct keyword with the type name? Example:

typedef struct {
    int x;
    int y;
}TypeName;


int main()
{

    TypeName instance; // isn't this the same as writing struct instance; ?

    return 0;
}

So how does typedef work when you use it on a struct?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It defines a struct with no name and then make TypeName a name for that same struct.

If you like, you can imagine it as if the compiler creates a random name for you (some compilers actually do):

typedef struct __TOTALLY_RANDOM_NAME_wieryweuoi3u4t23423cogh234283 {
    int x;
    int y;
}TypeName;

int main()
{
    struct __TOTALLY_RANDOM_NAME_wieryweuoi3u4t23423cogh234283 instance;

    return 0;
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading