Skip to main content

Structures and Typedef in C

I'll use this post to talk about creation & initialization of structures and also about the importance of using typedef while working with structures in C language. Structures are basically a data type which can be used to store group of objects of same or different data types as opposed to arrays which can store group of same data type only.

# General Syntax
struct tag_name {
    type member1;
    type member2;
};

In the above case, struct is the keyword that is used to define structures, tag_name is an optional argument. Lets take an example to understand this better

# Create a type "struct Point", which we can use later to declare variables of this type
struct Point {
    int x;
    int y;
};

# Create two variables pt1 and pt2 of type "struct Point"
struct Point pt1, pt2;

#Creation of type and variable declaration can be merged as well. It will basically create a type of struct Point and create two variables of this type
struct Point {
    int x;
    int y;
}pt1,pt2;

# Create more variables of same type
struct Point pt3, pt4;

# Create variable and initialize it
struct Point pt5 = {1 , 2};


Now as you noticed above, we are creating a struct Point type and using it to define variables of this type. In this case, Point is actually being used as an tag. We can also create a structure without a tag name but in that case, the structure definition can not be reused i.e. we can create variables only at the time of structure definition and can not create new variables later on.

# Create structure type without tag name
struct {
    int x;
    int y;
}pt1,pt2;

Let's quickly look at the typedef now. A typedef is used to alias an existing data type.

# Alias integer data type with newinteger
typedef int newint;
newint x=5;

As you would have noticed that while declaring variables of struct Point type, we would have to use struct keyword every time. So, we can actually use the typedef to alias struct Point so that we don't have to use struct keyword all the times.

# Use typedef with struct

# Option 1 : Use typedef after structure declaration
struct Point {
    int x;
    int y;
}pt1,pt2;

typedef struct Point Point_t;
Point_t pt3, pt4;

# Option2 : Use typedef at the time of structure declaration (with tags)
typedef struct Point {
    int x;
    int y;
} Point_t;

Point_t pt1, pt2;

# Option3 : Use typedef at the time of structure declaration (without tags)
typedef struct {
    int x;
    int y;
} Point_t;

Point_t pt1, pt2;

NOTE : In the above statement when typedef is used with structure definition, Point_t after braces {} is an alias to struct Point and NOT a variable. As typedef takes two arguments - existing data type and new data type. If we are using typedef with structures, everything before braces is considered as an existing data type and after braces a new data type.
I want to cover one more thing related to structures and typedef which is while using structures for pointer based data types like linked list.

# Using typedef and struct for Linked List
typedef struct node {
    int value;
    struct node *next;
} LList;

LList *list_node;

A linked list node would typically hold data and a pointer to next node. In the above code, we have created a struct type LList which will be used to create nodes and *next (pointer to next node) is used within structure definition using "struct node type". Its worth mentioning here, that you can't have a normal variable of type struct node within itself. This is because structures can be copied as value, so the compiler needs to know the size of structure. Creating a variable of same type within a structure will create an endless recursion loop. However, we can create a pointer of same type. This is because the size of pointer is known to compiler, hence no error even when the struct type in itself is in-complete at that time.

Comments