C – structures in C programming with examples

by Chaitanya Singh

in c-programming

Structure is nothing but a group of different or same data types. While making an application in C programming language, you may come across a place wherein you may have found various variables related to each other, which doesn’t make sense without each other, however you had to store them in different-2 arrays as their data types were different. So to avoid such headache we have a powerful concept in C, which are structures.

We generally use struct keyword while using them, which is nothing but short form of structured data type.

How to create structure?

struct struct_name {
   DataType member1_name;
   DataType member2_name;
   DataType member3_name;
   …
}

Here struct_name can be anything of your choice. Member’s data type can be of any type, they can be same as well as different.

After this declaration struct struct_name would act as a data type.

How to create variable of a structure?

struct  struct_name  var_name;

How to access data members of a structure through a struct variable?

var_name.member1_name;
var_name.member2_name;
…

How to assign values to struct members?
There are three ways –
1) Using Dot(.) operator

var_name.memeber_name = value;

2) Whole structure’s variables assignment in one go –

struct struct_name var_name = 
{value for memeber1, value for memeber2 …so on for all the members}

3) Designated initializers – We will learn this at the end of this post.

Complete Example:

#include <stdio.h>
/* I have created structure of name – StudentData*/
struct StudentData{
    char *stu_name;
    int stu_id;
    int stu_age;
}

int main()
{
     /* student is a variable of data type – StudentData*/
     struct StudentData  student;

     /*below I’m accessing struct members through variable –student*/
     student = {"Chaitanya", 1234, 25};

     printf("Student Name is: %s", student.stu_name);
     return 0;
}

There are two things to notice in above example –
1) Both the below code snippets are same.

student = {"Chaitanya", 1234, 25};

OR(both are same!!)

student.stu_name = "Chaitanya";
student.stu_id = 1234;
student.stu_age = 25;

2) Dot (.) operator can be used to assign values to individual members of structure or can be used to access values stored in structure’s individual members.

Struct inside another struct

You can use a structure inside another structure, which is fairly possible. As I explained you above that once you declared a structure, the struct struct_name would act as a new data type so you can include it in another struct just like a normal data member of any data type. Sounds confusing? Don’t worry. Below example would clear your doubts –

Structure 1:

struct stu_addres
{
     int street;
     char *state;
     char *city;
     char *country;
}

Structure 2:

struct stu_data
{
    int stu_id;
    int stu_age;
    char *stu_name;
    struct stu_address stuAddress;
}

Observe above code – I have nested a structure inside another structure. With the above example you would have understood the need also

Assignment for struct inside struct (Nested struct)

If I take the above example then assignment of values would happen in this way –

struct  stu_data  mydata = 
{123, 25, “Chaitanya”, 55, {“UP”, “Delhi”, “India”}}

Did you notice braces inside braces? Nested values are for nested struct’s members.

How to access nested struct members?
Using chain of “.” operator.
Suppose you want to display the city alone from nested struct –

printf("%s",  mydata.stuAddress.city);

Above I have used like this struct_variable.nested_struct_variable.member_name.

Use of typedef

typedef makes the code short and improves readability. In the above discussion we have seen that while using structs every time we have to use the lengthy syntax, which makes the code confusing, lengthy, complex and less readable. The simple solution to this issue is use of typedef. It is like an alias of struct.

Code without typedef

struct home_address {
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
};

…

…

struct home_address var1 = {55, "Dayal bagh", "Agra", "India"};

Code using tyepdef

typedef struct home_address{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
}addr;
..
..
addr var1 = {55, "Dayal bagh", "Agra", "India"};

What did you find above?
I could see that instead of using struct home_address, I can use addr(type name) anywhere in the code.

Remember the typedef syntax –

typedef before struct keyword and the type name after closing braces and before semicolon(;).

Designated initializers to set values

We have already learnt two ways to set the values of a struct member, there is another way to do the same using designated initializers.

typedef struct home_address{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
}addr;

addr var = {.city = "Agra", .street =55};

It is useful when you want to initialize few variables in struct.

A gist of above discussion
1) A struct is a collection of other data types.
2) The length of a struct is fixed based on the data type of the members.
3) A struct can be nested.
4) typedef creates a type name for a struct and makes the code easier and readable.
5) struct fields can be accessed using Dot(.) operator.

{ 0 comments… add one now }

Leave a Comment

Our Friend's website link : FromDev.com