C Programming Tutorial
About Lesson

C Structure

In this tutorial, you’ll learn about struct(structures) in C Programming, what is it, how to define it and use it in your program with the help of examples.


Structure

A struct (or structure) is a group or collection of variables (can be of different types) represented by a single name.

We use struct keyword to create a structure in C programming.

The struct keyword is a short form of structured data type.


What do you meant by Structure?

  • A structure is a user-defined data type in C.
  • A structure creates a data type that can be used to group items of possibly different types into a single line.
  • Structure are different form array because arrays only hold data of similar data types, on the other hand structure can store data of multiple data types.
  • Each element in the structure is called a member.

For example:

  • You want to store some information about someone: their name, citizenship and age.
  • You can easily create different variables name, citz, age to store these information separately
  • However, you may need to store information about many persons in the future. Now, you’d need to create different variables for each information per person: name1, citz1, age1, name2, citz2, age2.
  • You can easily visualize how big and messy the code would look.
  • Also, since no relation between the variables (information) would exist, it’s going to be a daunting task.

A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient a well.

This collection of all related information under a single name Person is a structure.


Syntax

A structure is defined with the struct keyword. A structure is a possible collection of primary data types and other structures.

  • The structure_name holds the name we want to give to our structure.
  • data_type variable is the C variables of different data types like intcharfloat, etc.
struct structure_name {
// data_type variable 1
// data_type variable 2
// data_type variable 3
...
};

Structure in C can contain two types of members:

  • Data Member: These members are normal C variables. we can create a structure with variables of different data types in C.
  • Member Functions: These members are normal C functions. Along with variables. we can also include functions inside a structure declaration.

Example

// Data Members
struct student {
  char name[20];
  int rollNo;
  float age;
};

How to declare a structure in C programming?

  • The struct keyword defines a structure type followed by an identifier (name of the structure).
  • The struct members are added within curly braces. These members probably belong to different data types.

For example:

struct student {
  char name[20];
  int rollNo;
  float age;
};

How to access members of a structure?

The Structure members are accessed using dot (.) operator.

Syntax:

variableName.member2;

Suppose, you want to access age of structure variable p and assign it 20 to it. You can perform this task by using following code below:

p.age = 20;

Example : Access members of a structure

// Program to print the data of a student
#include <stdio.h>
struct studentData{
  char *name;
  int rollNo;
  int age;
} student;

int main() {
  //Defining variable student of structure studentData
  struct studentData student;
  //Assigning the values of each studentData members
  student.name = "Tony";
  student.rollNo = 21;
  student.age = 15;
  printf("Student name is %s\n",student.name);
  printf("Student Roll No. is %d\n",student.rollNo);
  printf("Student age is %d\n",student.age);
  return 0;
}

Output

Student name is Tony
Student Roll No. is 21
Student age is 15

Nested Structure

We can create a structure inside another structure in C programming.

Example

struct studentAddress {
  int street;
  char *state;
  char *city;
  char *country;
};
...
...
...
...
struct studentData{
  char *name;
  int rollNo;
  int age;
  struct studentData student;
};

 

accessing nested structure

studentData.studentAddress.city = "London";

Here, we have the city member of studentAddress structure which is nested in studentData structure.


Reason for using structure ?

Suppose, we want to to store the data of a student like nameageidaddress etc then we have to declare different variable for each attribute.
But if we want to store the data of more that one student then we have to declare those variable again for each student.
We can easily solve this problem by using structure. We can create a structure that has members for nameageidaddress etc and then we have to create the variables for each student.


typedef

typedef is used to create an alias name for datatypes i.e. an alias of struct . It helps us to make the code short and improve the readability.

Code without typedef

struct studentData{
  char *name;
  int rollNo;
  int age;
};
...
...
...
...
struct studentData student1;
student1.name = "Tony";

Code using typedef

typedef struct studentData {
  int feet;
float inch;
} student;
...
...
...
...
student student1;
student1.name = "Tony";

Here, we have observed that instead of using struct student Data every time we can declare struct variable student with the help of typedef that we have defined.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!