0% found this document useful (0 votes)
4 views8 pages

C Structures Notes

Structures in C are custom data types that group different types of variables together under one name, similar to an ID card containing a name, roll number, and marks. They allow for organized data management, enabling the creation of arrays of structures, nested structures, and easy data passing to functions. While structures provide advantages in code organization and readability, they also consume more memory and can be complex for beginners.

Uploaded by

adarshsingh9674
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

C Structures Notes

Structures in C are custom data types that group different types of variables together under one name, similar to an ID card containing a name, roll number, and marks. They allow for organized data management, enabling the creation of arrays of structures, nested structures, and easy data passing to functions. While structures provide advantages in code organization and readability, they also consume more memory and can be complex for beginners.

Uploaded by

adarshsingh9674
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structures in C

Structures in C — Simple Notes

1. What is a Structure? (The Easy Way)

Remember how an array is a row of lockers that all hold the same type of thing (all numbers, or all characters)?

A Structure is different — it’s like one single locker box that holds different types of items together, like a
school ID card:

┌─────────────────────────────┐
│ STUDENT ID CARD │
│ Name : "Ravi" (text) │
│ Roll : 21 (number) │
│ Marks: 89.5 (decimal) │
└─────────────────────────────┘

One card, but it holds a name (text), a roll number (integer), and marks (decimal) — all different types,
grouped under one single name.

Structure = A custom data type that groups different types of variables together under one name.

Why do we need structures?

Without structures, you’d need separate messy arrays:

char names[50][20];
int rolls[50];
float marks[50];

Confusing — which name belongs to which roll number? With a structure, everything about one student stays
neatly together in one box.

2. Defining a Structure (Designing the ID Card)

struct StructureName {
dataType member1;
dataType member2;
dataType member3;
};

Example:

struct Student {
char name[20];
int roll;
float marks;
};

⚠ Important: This only designs the card — it doesn’t create an actual student yet! It’s like a blank template.

3. Creating Structure Variables (Printing Actual Cards)


Method 1 – Declare separately

struct Student s1; // s1 is now an actual card, ready to be filled

Method 2 – Declare while defining

struct Student {
char name[20];
int roll;
float marks;
} s1, s2; // create two cards immediately

4. Filling in the Structure (Writing on the Card)

Use the dot operator ( . ) to access each member (each field on the card):

#include <stdio.h>
#include <string.h>

struct Student {
char name[20];
int roll;
float marks;
};

int main() {
struct Student s1;

strcpy([Link], "Ravi"); // strings need strcpy, not '='


[Link] = 21;
[Link] = 89.5;

printf("Name: %s\n", [Link]);


printf("Roll: %d\n", [Link]);
printf("Marks: %.1f\n", [Link]);

return 0;
}

Output:

Name: Ravi
Roll: 21
Marks: 89.5

5. Initializing a Structure While Declaring

Just like filling an array, you can fill the whole card at once:

struct Student s1 = {"Ravi", 21, 89.5};

The order must match the order of members you defined: name , then roll , then marks .

6. Structures and Arrays (A Whole Class of Cards!)

Just like an array of numbers, you can make an array of structures — a stack of ID cards, one per student:
struct Student students[3] = {
{"Ravi", 21, 89.5},
{"Priya", 22, 92.0},
{"Aman", 23, 76.5}
};

Printing all cards using a loop:

#include <stdio.h>

struct Student {
char name[20];
int roll;
float marks;
};

int main() {
struct Student students[3] = {
{"Ravi", 21, 89.5},
{"Priya", 22, 92.0},
{"Aman", 23, 76.5}
};
int i;

for (i = 0; i < 3; i++) {


printf("Name: %s, Roll: %d, Marks: %.1f\n",
students[i].name, students[i].roll, students[i].marks);
}
return 0;
}

Output:

Name: Ravi, Roll: 21, Marks: 89.5


Name: Priya, Roll: 22, Marks: 92.0
Name: Aman, Roll: 23, Marks: 76.5

7. How Structures Sit in Memory

Unlike arrays (all same-size boxes), structure members can be different sizes, but they’re still stored next to
each other in memory, in the order you defined them:

struct Student s1:

name[20] roll (int) marks (float)


[ R a v i \0...][ 21 ][ 89.5 ]
20 bytes 4 bytes 4 bytes

8. Nested Structures (A Card Inside a Card)

A structure can contain another structure as a member — like an ID card that has a small “Date of Birth” card
stapled inside it.
struct Date {
int day;
int month;
int year;
};

struct Student {
char name[20];
int roll;
struct Date dob; // structure inside a structure!
};

Accessing nested members — just chain the dots:

struct Student s1;


[Link] = 15;
[Link] = 8;
[Link] = 2005;

printf("%d-%d-%d", [Link], [Link], [Link]);

9. Structures and Functions

You can pass a whole structure to a function — like handing someone the entire ID card at once, instead of each
detail separately.

#include <stdio.h>

struct Student {
char name[20];
int roll;
float marks;
};

void displayStudent(struct Student s) {


printf("Name: %s, Roll: %d, Marks: %.1f\n", [Link], [Link], [Link]);
}

int main() {
struct Student s1 = {"Ravi", 21, 89.5};
displayStudent(s1); // pass the whole card
return 0;
}

⚠ Note: This passes a copy of the structure (call by value). Changes inside the function won’t affect the original —
just like with normal variables.

10. Structures and Pointers (Fast-Track Access)

Remember pointers from the last topic? You can make a pointer point to a structure too — like a map pointing
directly to the ID card instead of carrying a photocopy of it.
#include <stdio.h>

struct Student {
char name[20];
int roll;
float marks;
};

int main() {
struct Student s1 = {"Ravi", 21, 89.5};
struct Student *ptr = &s1; // ptr points to s1

// Use the arrow operator '->' instead of dot when using a pointer!
printf("Name: %s\n", ptr->name);
printf("Roll: %d\n", ptr->roll);
printf("Marks: %.1f\n", ptr->marks);

return 0;
}

Access Type Symbol to Use

Normal variable [Link] (dot)

Pointer to structure ptr->roll (arrow)

ptr->roll is just a shortcut for (*ptr).roll

Passing structure by pointer to a function (so changes actually stick!)

#include <stdio.h>

struct Student {
char name[20];
int roll;
float marks;
};

void updateMarks(struct Student *s, float newMarks) {


s->marks = newMarks; // changes the ORIGINAL structure
}

int main() {
struct Student s1 = {"Ravi", 21, 89.5};
updateMarks(&s1, 95.0);
printf("Updated Marks: %.1f\n", [Link]); // 95.0
return 0;
}

11. typedef with Structures (Giving a Shortcut Nickname)

Typing struct Student every time is tiring. typedef lets you give it a short nickname:
typedef struct {
char name[20];
int roll;
float marks;
} Student;

int main() {
Student s1 = {"Ravi", 21, 89.5}; // no need to write "struct" anymore!
printf("%s", [Link]);
return 0;
}

12. Structure vs Array — What’s the Difference?

Array Structure

All elements are the same type Members can be different types

Accessed using index arr[i] Accessed using member name [Link]

Like a row of identical lockers Like one ID card with mixed fields

13. Rules to Remember (Important!)

Rule Meaning

struct keyword Needed to define and declare (unless using typedef )

Dot . for normal variables [Link]

Arrow -> for pointers ptr->roll

Use strcpy for string members Can’t use = directly on char arrays

Order matters in initialization {val1, val2, val3} must match member order

Can nest structures A structure can contain another structure

Passed by value by default Use pointers to actually modify the original in a function

14. Advantages of Structures

Groups related but different-type data together neatly


Makes code more organized and readable (one student = one unit)
Easy to pass a whole group of data to a function at once
Foundation for advanced topics like linked lists, trees, and files

15. Disadvantages of Structures

Takes more memory than simple variables (stores all members together)
Passing large structures by value (copying) can be slow — use pointers instead
Slightly more complex for beginners compared to plain variables

16. One Full Example Program (Putting it all together)


#include <stdio.h>
#include <string.h>

struct Student {
char name[20];
int roll;
float marks;
};

void displayStudent(struct Student *s) {


printf("Name: %-10s Roll: %-5d Marks: %.1f\n", s->name, s->roll, s->marks);
}

void updateMarks(struct Student *s, float newMarks) {


s->marks = newMarks;
}

int main() {
struct Student students[3] = {
{"Ravi", 21, 89.5},
{"Priya", 22, 92.0},
{"Aman", 23, 76.5}
};
int i;

printf("---- Before Update ----\n");


for (i = 0; i < 3; i++) {
displayStudent(&students[i]);
}

updateMarks(&students[0], 95.0); // update Ravi's marks

printf("\n---- After Update ----\n");


for (i = 0; i < 3; i++) {
displayStudent(&students[i]);
}

return 0;
}

Output:

---- Before Update ----


Name: Ravi Roll: 21 Marks: 89.5
Name: Priya Roll: 22 Marks: 92.0
Name: Aman Roll: 23 Marks: 76.5

---- After Update ----


Name: Ravi Roll: 21 Marks: 95.0
Name: Priya Roll: 22 Marks: 92.0
Name: Aman Roll: 23 Marks: 76.5

Quick Recap (In One Line Each)

1. Structure = one box holding different types of data together, like an ID card.
2. Define: struct Student { char name[20]; int roll; float marks; };
3. Declare: struct Student s1;
4. Access members with . (dot): [Link]
5. Access via pointer with -> (arrow): ptr->roll
6. Array of structures = a whole stack of ID cards.
7. Structures can nest inside other structures.
8. Passed to functions by value (copy) unless you pass a pointer ( &s1 ).
9. typedef gives structures a shorter nickname to avoid repeating struct .
That’s it — you now know Structures in C!

You might also like