C

Examples Of Structure In C Programming

A structure is a collection of variables of various data types that are identified by a single name. Let’s look at an example to see why a structure is necessary in C programming.

Why is structure required in C?

Assume we need to store student data such as name, age, address, ID, and so on. One method would be to create a separate variable for each attribute; however, if you need to store the data of multiple students, you would need to create these several variables again for each student. This method of data storage is extremely inconvenient.
We can easily solve this problem by employing structure. We can create a structure with members for name, ID, address, and age, and then we can create variables for each student in this structure. This may appear confusing, but don’t worry, we’ll explain it with an example.

In C programming, how do you make a structure?

To create a structure in C, we use the struct keyword. The struct keyword is an abbreviation for structured data type.

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

The name of the structure, struct name, is chosen by the programmer and can be anything. However, always use meaningful, short, and easy-to-read names and avoid using keywords or reserved words as structure names, which are not permitted. The data types of members can be the same or different. Once the structure has been declared, we can use the struct name as a data type like int, float, and so on.
We will first see the syntax for creating struct variables, accessing struct members, and so on, followed by a complete example.

How do I declare a structure variable?

struct  struct_name  var_name;

or

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

How do I use a struct variable to access data members in a structure?

How should structure members be assigned values?

var_name.member1_name;
var_name.member2_name;

There are three alternatives.

1) Employing the Dot(.) operator

var_name.memeber_name = value;

2) All members are assigned in a single statement.

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

3) Designated initializers – This will be covered further at the end of this post.

Structure in C Example

In this example, we’ve created the structure StudentData, which contains three data members: stu name, stu id, and stu age. In this program, we store the student name, ID, and age in a structure and then access structure data members to display these values as output.

#include <stdio.h>
/* Created a structure here. The name of the structure is
 * StudentData.
 */
struct StudentData{
    char *stu_name;
    int stu_id;
    int stu_age;
};
int main()
{
     /* student is the variable of structure StudentData*/
     struct StudentData student;

     /*Assigning the values of each struct member here*/
     student.stu_name = "Steve";
     student.stu_id = 1234;
     student.stu_age = 30;

     /* Displaying the values of struct members */
     printf("Student Name is: %s", student.stu_name);
     printf("\nStudent Id is: %d", student.stu_id);
     printf("\nStudent Age is: %d", student.stu_age);
     return 0;
}

Output:

Student Name is: Steve
Student Id is: 1234
Student Age is: 30

In C, a nested structure is a structure that is inside another structure.

Nesting of structures is the use of one structure inside another. As previously stated, once a structure is declared, the struct struct name acts as a new data type that can be included in another struct just like the data type of other data members. Does that sound perplexing? Don’t worry. The following example will dispel your doubt.

Nested Structure in C Programming

Assume we have two structures that look like this: The second structure stu data contains the data member stu address. In this context, stu data is referred to as the outer structure or parent structure, while stu address is referred to as the inner structure or child structure.
Structure 1: stu address

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

Structure 2: stu_data

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

As you can see, I’ve nested a structure within another structure.

Struct assignment within struct (Nested struct)

To better understand the logic, consider the two structures shown above. We are employing the. To access the data member of the inner structure, use the (dot) operator twice.

struct  stu_data  mydata;
mydata.stu_id = 1001;
mydata.stu_age = 30;
mydata.stuAddress.state = "UP"; //Nested struct assignment
..

Similarly, other data members of the inner structure can be accessed in the same manner:

mydata.stuAddress.city = "Delhi";
mydata.stuAddress.country = "India";

How do I get to the members of a nested structure?

Using the “.” operator in a chain. Assume you want to show just the city from a nested struct:

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

Let’s go over the entire schedule:

#include <stdio.h>
struct stu_address
{
  int street;
  char *state;
  char *city;
  char *country;
};
struct stu_data
{
  int stu_id;
  int stu_age;
  char *stu_name;
  struct stu_address stuAddress;
};
int main(){
  struct stu_data mydata;
  mydata.stu_id = 1001;
  mydata.stu_age = 30;
  mydata.stu_name = "Chaitanya";
  mydata.stuAddress.state = "UP";
  mydata.stuAddress.street = 101;
  mydata.stuAddress.city = "Delhi";
  mydata.stuAddress.country = "India";
  printf("Printing student Data: ");
  printf("\nStudent id: %d",mydata.stu_id);
  printf("\nStudent age: %d",mydata.stu_age);
  printf("\nStudent name: %s",mydata.stu_name);
  printf("\nStudent street: %d",mydata.stuAddress.street);
  printf("\nStudent state: %s",mydata.stuAddress.state);
  printf("\nStudent city: %s",mydata.stuAddress.city);
  printf("\nStudent country: %s",mydata.stuAddress.country);

  return 0;
}

Structure’s use of typedef

typedef shortens the code and improves readability. In the preceding discussion, we saw that when using structs, we must always use the lengthy syntax, which makes the code confusing, lengthy, complex, and less readable. The use of typedef is a simple solution to this problem. It’s similar to an alias for struct.
Typedef-free code

struct home_address {
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
};
...
struct home_address var; 
var.town = "Agra";

Use tyepdef to write code

typedef struct home_address{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
}addr;
..
..
addr var1;
var.town = "Agra";

You can use addr, the typedef that we defined, instead of struct home address every time you need to declare a struct variable.
The typedef can be found here in its entirety.

Structures Array in C

An array of structures is a collection of structures as elements.
As an example:
In this case, stu[5] is an array of structures. This array contains 5 elements, all of which are structures of the same type “student.” The element s[0] will store values such as a student’s name, rollNum, address, and marks, while element s[1] will store these details for another student, and so on.

struct student {
  char name[60];
  int rollNum;
  char address[60];
  float marks;
} stu[5];

Structure members’ values are set by designated initializers.

We have already learned two methods for setting the values of struct members; there is another method that uses designated initializers. This is useful when only a few members of the structure need to be assigned. The structure variable s2 in the following example has only one member assignment.

#include <stdio.h>
struct numbers
{
   int num1, num2;
};
int main()
{
   // Assignment using using designated initialization
   struct numbers s1 = {.num2 = 22, .num1 = 11};
   struct numbers s2 = {.num2 = 30};
 
   printf ("num1: %d, num2: %d\n", s1.num1, s1.num2);
   printf ("num1: %d", s2.num2);
   return 0;
}

Output:

num1: 11, num2: 22
num1: 30

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button