Structures
Structure is a user-defined data type that can be used to group items of
possibly different types into a single type.
● The struct keyword is used to define a structure. The items in the
structure are called its members and they can be of any valid data
type.
● Applications of structures involve creating data structures Linked
List and Tree. Structures are also used to represent real world
objects in a software like Students and Faculty in a college
management software.
#include <stdio.h>
// Defining a structure
struct A {
int x;
};
int main() {
// Creating a structure variable
struct A a;
// Initializing member
a.x = 11;
printf("%d", a.x);
return 0;
}
Basic Operations of Structure
1. Access Structure Members
● To access or modify members of a structure, we use the ( . ) dot
operator. This is applicable when we are using structure variables
directly.
● In the case where we have a pointer to the structure, we can also
use the arrow operator to access the members.
2. Initialize Structure Members
● Structure members cannot be initialized with the declaration. For
example, the following C program fails in the compilation.
#include <stdio.h>
// Defining a structure to represent a student
struct Student
{
char name[50];
int age;
float grade;
};
int main()
{
// Declaring and initializing a structure variable
struct Student s1 = {"Rahul", 20, 18.5};
// Designated Initializing another structure
struct Student s2 = {.age = 18, .name = "Vikas", .grade = 22};
// Accessing structure members
printf("%st%dt%.2fn", s1.name, s1.age, s1.grade);
printf("%st%dt%.2fn", s2.name, s2.age, s2.grade);
return 0;
}
Output
Rahul 20 18.50
Vikas 18 22.00

Structures and basic Operations in C Programming Language

  • 1.
    Structures Structure is auser-defined data type that can be used to group items of possibly different types into a single type. ● The struct keyword is used to define a structure. The items in the structure are called its members and they can be of any valid data type. ● Applications of structures involve creating data structures Linked List and Tree. Structures are also used to represent real world objects in a software like Students and Faculty in a college management software. #include <stdio.h> // Defining a structure struct A { int x; }; int main() { // Creating a structure variable struct A a; // Initializing member a.x = 11; printf("%d", a.x); return 0; } Basic Operations of Structure 1. Access Structure Members
  • 2.
    ● To accessor modify members of a structure, we use the ( . ) dot operator. This is applicable when we are using structure variables directly. ● In the case where we have a pointer to the structure, we can also use the arrow operator to access the members. 2. Initialize Structure Members ● Structure members cannot be initialized with the declaration. For example, the following C program fails in the compilation. #include <stdio.h> // Defining a structure to represent a student struct Student { char name[50]; int age; float grade; }; int main() { // Declaring and initializing a structure variable struct Student s1 = {"Rahul", 20, 18.5}; // Designated Initializing another structure struct Student s2 = {.age = 18, .name = "Vikas", .grade = 22}; // Accessing structure members printf("%st%dt%.2fn", s1.name, s1.age, s1.grade); printf("%st%dt%.2fn", s2.name, s2.age, s2.grade); return 0; }
  • 3.