Home > Learning > typedef and structs

typedef and structs

Another thing I learned in the C introduction to “Advanced C++ Programming”, typedef used with structs. Every teacher (and everyone else) I asked before kept saying typedef was “sorta complicated”, which meant they either didn’t know or didn’t want to explain it to me.

As I understand it, typedef is an instruction which tells the compiler to treat what is defined as a type.

For example, given a defined structure:

struct MyStruct
    int var1;
    char var2;

Using this struct means carrying the struct keyword everywhere.

//Anywhere it is declared
struct MyStruct my;
//And in function parameters too
void MyFunc(struct MyStruct my);

typedef simplifies this greatly:

typedef struct
    int var1;
    char var2;
} MyStruct;

MyStruct my;
//Parameter declaration
void MyFunc(MyStruct my);
Categories: Learning Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: