Apply for Zend Framework Certification Training

c




< Memory Leaks - Understanding and Avoiding Last >



Structure in C
 
1. What is a Structure in C?
A structure is a user-defined data type that allows you to store different types of data in one unit.
Example: A student has:
Name (string)
Roll number (int)
Marks (float)
Instead of separate variables, we combine them using a structure.
 
2. Defining a Structure
Syntax:
struct structure_name {
    data_type member1;
    data_type member2;
    ...
};
Example:
struct Student {
    int roll;
    char name[50];
    float marks;
};
 
3. Declaring Structure Variables
Method 1: After structure definition
struct Student s1, s2;
Method 2: During definition
struct Student {
    int roll;
    char name[50];
    float marks;
} s1, s2;
 
4. Accessing Structure Members
Use dot operator (.)
Example:
s1.roll = 101;
strcpy(s1.name, "Raj");
s1.marks = 85.5;
Full Program:
#include <stdio.h>
#include <string.h>
struct Student {
    int roll;
    char name[50];
    float marks;
};
int main() {
    struct Student s1;
    s1.roll = 1;
    strcpy(s1.name, "Amit");
    s1.marks = 90.5;
    printf("Roll: %d\n", s1.roll);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks);
    return 0;
}
 
5. Arrays of Structures
Used when handling multiple records
Example:
#include <stdio.h>
struct Student {
    int roll;
    float marks;
};
int main() {
    struct Student s[3];
    int i;
    for(i = 0; i < 3; i++) {
        printf("Enter roll and marks: ");
        scanf("%d %f", &s[i].roll, &s[i].marks);
    }
    printf("\nStudent Details:\n");
    for(i = 0; i < 3; i++) {
        printf("Roll: %d, Marks: %.2f\n", s[i].roll, s[i].marks);
    }
    return 0;
}
 
6. Pointers to Structures
Use arrow operator (->)
Example:
#include <stdio.h>
struct Student {
    int roll;
    float marks;
};
int main() {
    struct Student s1 = {1, 88.5};
    struct Student *ptr;
    ptr = &s1;
    printf("Roll: %d\n", ptr->roll);
    printf("Marks: %.2f\n", ptr->marks);
    return 0;
}
Equivalent:
(*ptr).roll
 
7. Nested Structures
Structure inside another structure
struct Date {
    int day, month, year;
};
struct Student {
    int roll;
    struct Date dob;
};
 
8. Structure vs Array
Feature Structure Array
Data Type Different Same
Example int + float + char only int OR only float
Access dot operator index
 
9. Real-Life Example
 
Think of a Student Form
Name
Roll No
Marks
Structure = One form
Array of structures = Many students
 
PRACTICE PROBLEMS + SOLUTIONS
 
Problem 1: Store and Display Student Info
Solution:
#include <stdio.h>
struct Student {
    int roll;
    float marks;
};
int main() {
    struct Student s;
    printf("Enter roll and marks: ");
    scanf("%d %f", &s.roll, &s.marks);
    printf("Roll: %d, Marks: %.2f", s.roll, s.marks);
    return 0;
}
Problem 2: Store Data of 5 Students
#include <stdio.h>
struct Student {
    int roll;
    float marks;
};
int main() {
    struct Student s[5];
    int i;
    for(i = 0; i < 5; i++) {
        printf("Enter roll and marks: ");
        scanf("%d %f", &s[i].roll, &s[i].marks);
    }
    printf("\nDetails:\n");
    for(i = 0; i < 5; i++) {
        printf("%d %.2f\n", s[i].roll, s[i].marks);
    }
    return 0;
}
 
Problem 3: Use Pointer to Structure
#include <stdio.h>
struct Student {
    int roll;
};
int main() {
    struct Student s = {10};
    struct Student *ptr = &s;
    printf("Roll: %d", ptr->roll);
    return 0;
}
 
Problem 4: Find Highest Marks
#include <stdio.h>
struct Student {
    int roll;
    float marks;
};
int main() {
    struct Student s[3];
    int i;
    float max;
    for(i = 0; i < 3; i++) {
        scanf("%d %f", &s[i].roll, &s[i].marks);
    }
    max = s[0].marks;
    for(i = 1; i < 3; i++) {
        if(s[i].marks > max) {
            max = s[i].marks;
        }
    }
    printf("Highest Marks = %.2f", max);
    return 0;
}
 
Problem 5: Pass Structure to Function
#include <stdio.h>
struct Student {
    int roll;
};
void display(struct Student s) {
    printf("Roll: %d", s.roll);
}
int main() {
    struct Student s1 = {5};
    display(s1);
    return 0;
}

< Memory Leaks - Understanding and Avoiding Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top