슈뢰딩거의 고등어

Difference between typedef struct & struct 본문

tech

Difference between typedef struct & struct

슈뢰딩거의 고등어 2022. 1. 5. 17:53

how to define Struct?

struct SuperMan { 
	int power; 
	int age; 
};

We can define a Struct named SuperMan like this. But, actually, this doesn't create struct SuperMan itself. It just lets Compiler know how struct SuperMan looks like.

If you want to define a Struct variable that has real memory, you should define it like this.

struct SuperMan ClarkKent;

OR you can make it shorter.

struct SuperMan { 
	int power; 
    int age; 
}ClarkKent;

AS you know, defining struct means creating a new data type. 

Defining struct variable means giving it to the new memory space.

 

 

WHAT is typedef??

typedef gives a nametag to a certain data type. 

For example, there is a data type named unsigned short int. Typing it every time is exhausting or may cause errors. What if it has another short nickname? So we can give it a nickname like UINT16. 

typedef unsigned short int UINT16;

 

WHAT is typedef struct??

typedef + [type] + [nickname]

typedef struct SuperMan { 
	int power; 
    int age; 
} sman_t;

 

 

[type] : struct SuperMan {}

[nickname] : sman_t;

Therefore, We can call defining struct SuperMan to sman_t;

Now we can define a variable like

sman_t ClarkKent;

 

So why we use typedef struct is We can make it shorter.

Comments