Understanding Structures in C
Programming
What is a Structure?
• A user-defined data type that allows grouping
of different types of variables.
• Useful for organizing complex data.
Why Use Structures?
Advantages:
• - Organizes data logically.
• - Makes code more readable and manageable.
• - Enables easier data handling for complex
data types (like a student record).
Syntax of a Structure
• Defining a Structure:
struct StructureName {
dataType member1;
dataType member2;
};
• Example:
struct Student {
char name[50];
int age;
float grade;
};
Accessing Structure Members
• Accessing members using the dot operator:
• s1.age = 20;
• s1.grade = 85.5;
Modifying Structure Members
• Use a function to modify:
void modifyStudent(struct Student *s, const
char *newName, int newAge, float newGrade) {
strcpy(s->name, newName);
s->age = newAge;
s->grade = newGrade;
};
Printing Structure Contents
• Function to print student details:
void printStudent(struct Student s) {
printf("Name: %s, Age: %d, Grade: %.2fn",
s.name, s.age, s.grade);
};
Nested Structures
• Define Address structure:
struct Address {
char city[50];
char state[50];
};
• Define Student structure:
struct Student {
char name[50];
int age;
struct Address addr;
};

Powerpoint presentation on structures in C programing

  • 1.
  • 2.
    What is aStructure? • A user-defined data type that allows grouping of different types of variables. • Useful for organizing complex data.
  • 3.
    Why Use Structures? Advantages: •- Organizes data logically. • - Makes code more readable and manageable. • - Enables easier data handling for complex data types (like a student record).
  • 4.
    Syntax of aStructure • Defining a Structure: struct StructureName { dataType member1; dataType member2; }; • Example: struct Student { char name[50]; int age; float grade; };
  • 5.
    Accessing Structure Members •Accessing members using the dot operator: • s1.age = 20; • s1.grade = 85.5;
  • 6.
    Modifying Structure Members •Use a function to modify: void modifyStudent(struct Student *s, const char *newName, int newAge, float newGrade) { strcpy(s->name, newName); s->age = newAge; s->grade = newGrade; };
  • 7.
    Printing Structure Contents •Function to print student details: void printStudent(struct Student s) { printf("Name: %s, Age: %d, Grade: %.2fn", s.name, s.age, s.grade); };
  • 8.
    Nested Structures • DefineAddress structure: struct Address { char city[50]; char state[50]; }; • Define Student structure: struct Student { char name[50]; int age; struct Address addr; };