Structures in C

You might have heard about linked list. It is an efficient data structure as it can grow and shrink. Memory is allocated dynamically. The most important thing is that for using an awesome data structure like linked list we need to create a structure for that linked list first.

What is a Structure?

If we want to deal with integer then we create an int. If we wish to deal with character then we use char. But in real world scenario we are dealing with more complex type of data which can include combination of many things.

For example, if we deal with storing information about food items then we need

  1. Name of food item - an array of character.
  2. Price of food item -  a float.
  3. Unique id of that item - an integer.

So how to hold this kind of data?

Smart answer : By using structures. 

Here comes the structure baby.

struct food{

       char name[10];
       float price;
       int id;

It is a unique data type which we have created.

How can we use structure?

Easy!! It can be used like any other data type.

struct food f1 = {"pizza", 101.2, 101};
struct food f2 = {"apple", 20, 102};
struct food f3 = {"mc-puff", 30, 103};

Now how can you access structure elements

The below statement will give you the value of the particular item of structure. Let us get the price of the first item.


Let us see structures in action

  • Take input from user.
  • Store it in structure.
  • Print items.
#include <stdio.h>
#include <string.h>
struct food{
    char name[10];
    float price;
    int id;

int main(void) {
struct food f1;
   scanf("%f%d", &f1.price, &;
   printf("Name : %s \nPrice: %f \n unique id :%d",, f1.price,;
	return 0;


Pointer to a structure

struct food f1 = {"pizza", 50.5, 101};
struct food *ptr;
ptr = &f1;

Here ptr is holding the address of the structure.

How to acces structure through a pointer


But generally we write it in a short form


So, we learned some basic concepts of structures. Now we have a strong foundation for learning linked list. We can proceed further to implement linked list in next tutorial.

Linked List in C