Structure in C Programming Language

Structure in C Programming Language: In C programming, a structure is a user-defined data type that allows the combination of different data types (int, float, char, etc.) under a single entity. It is particularly useful when dealing with data that requires grouping, such as representing information about a person (name, age, address) or an item in a database (product ID, price, and description). Unlike arrays, which store similar data types, structures allow the storage of varied data types in one composite format.

C Programming Video Tutorial Lecture-02

Structures in C provide a powerful way to group variables of different data types together, enabling you to model real-world data more effectively. By understanding how to define, access, and manipulate structures, you’ll be better equipped to handle more complex data in your C programs.

It is a collections of hetrogenous types of data.

It is keyword in c programming language to create structure i.e., struct keyword.

It is a user defined datatype.

Example:

struct student{

int Roll;

char name[20];

}

The above code is only called declaration or blueprint or template.

Here, No any momory will be allocated.

int main(){

struct student s1, s2;

}

The above code contain struct student is a data type or user defined data type and s1 & s2 is a veriables.

If we need to access a perticular number of a group we need to specify the group name.

Note: Membership Operator: ‘ .

struct student {

int Roll;

char name[20];

}

int main(){

struct student s1, s2;

s1.Roll = 5;

s2.Roll = 15;

}

To declare a structure in C, use the struct keyword followed by the structure definition:

struct structure_name {
data_type member1;
data_type member2;
// More members can be added here
};


Here, structure_name is the tag that will be used to refer to the structure, and member1, member2, etc., are the fields or members of the structure.

struct Person {
char name[50];
int age;
float height;
};

This example defines a structure Person with three members: name, age, and height.

Once a structure is defined, you can create variables of that type and access the structure members using the dot (.) operator.

struct Person person1;

person1.age = 25; // Accessing and setting the age member
strcpy(person1.name, “John Doe”); // Setting name using strcpy
person1.height = 5.9; // Setting height

printf(“Name: %s\n”, person1.name);
printf(“Age: %d\n”, person1.age);
printf(“Height: %.1f\n”, person1.height);

You can initialize structure members at the time of declaration:

struct Person person2 = {“Alice”, 30, 5.7};
This creates a Person structure variable person2 and initializes its members with the values “Alice”, 30, and 5.7.

You can also create arrays of structures, allowing you to store multiple instances of a structure.

Example of Array of Structure in C Programming Language

struct Person people[3] = {
{“John Doe”, 25, 5.9},
{“Alice”, 30, 5.7},
{“Bob”, 22, 6.0}
};

You can access individual elements using array indexing, for example:

printf(“First person: %s, %d, %.1f\n”, people[0].name, people[0].age, people[0].height);

Just like other data types, pointers can be used with structures. To access structure members using a pointer, you use the arrow (->) operator.

Whenever we have a pointer to structure then instead of doing (*ptr).a to access numbers named, we can also use arrow (->) operator.

ptr -> a equivalent to (*ptr).a

ptr -> b equivalent to (*ptr).b

Example of Pointer to Structure in C Programming Language

struct student{

char name[20];

int Roll;

}

int main(){

struct student s = {“Zee”, 1};

display(&s);

}

void display (struct student *ptr){

printf(“%s”, ptr -> name);

printf(“%d”, ptr -> Roll);

}

Example of Pointer to Structure in C Programming Language

struct Person *ptr, person3 = {“Emily”, 28, 5.8};
ptr = &person3;

printf(“Name: %s\n”, ptr->name);
printf(“Age: %d\n”, ptr->age);
printf(“Height: %.1f\n”, ptr->height);

Here, ptr is a pointer to the structure person3, and we use the -> operator to access the structure members.

Example of Nested Structure in C Programming Language

struct Address {
char street[50];
char city[50];
int zip;
};

struct Person {
char name[50];
int age;
struct Address address;
};

Here, the Person structure contains a nested Address structure.

Accessing Members of Nested Structure in C Programming Language

struct Person person4 = {“Michael”, 40, {“123 Main St”, “New York”, 10001}};

printf(“Street: %s\n”, person4.address.street);
printf(“City: %s\n”, person4.address.city);
printf(“ZIP: %d\n”, person4.address.zip);

  • Organizing Data: Structures allow the grouping of different data types, which improves code readability and management.
  • Flexibility: Structures provide flexibility to create complex data types that can represent real-world entities.
  • Memory Efficiency: Since structures don’t need extra memory for each member individually, they can be more efficient than arrays of different data types.

What is the difference between a structure and an array in C?

Arrays store elements of the same data type, whereas structures can store variables of different data types under one name.

Can we have an array of structures?

Yes, you can have an array of structures, allowing multiple instances of a structure to be stored in an indexed manner.

How are structures different from classes in object-oriented programming languages like C++?

Structures in C are primarily used for grouping different data types, while classes in OOP languages like C++ provide data encapsulation, inheritance, and other object-oriented features. Structures in C don’t support methods or private/public access modifiers.

Can a structure contain another structure?

Yes, structures can contain other structures as members. This is referred to as a nested structure.

Is it possible to pass structures to functions in C?

Yes, you can pass structures to functions either by value (copying the structure) or by reference (passing the address of the structure).
Previous

Learn More

Subscribe “JNG ACADEMY” for more learning best content.

C Programming Language Lecture-01

C Programming Language Lecture-02

C Programming Language Lecture-03

C Programming Language Lecture-04

C Programming Language Lecture-05

C Programming Language Youtube Playlist

Thank You So Much, Guys… For Visiting our Website and Youtube.

Leave a Comment