C Structures

1. What is a Structure?

  • A structure is a user-defined data type in C.

  • It allows you to store multiple variables of different data types under a single name.

  • Useful for representing complex objects like a student, employee, or point in 2D space.

Syntax:

struct structure_name {
data_type member1;
data_type member2;
...
};

2. Example: Defining a Structure

#include <stdio.h>

struct Student {
int id;
char name[50];
float marks;
};

int main() {
struct Student s1;

s1.id = 101;
strcpy(s1.name, "Alice"); // string copy
s1.marks = 95.5;

printf("ID: %d\n", s1.id);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);

return 0;
}

Output:

ID: 101
Name: Alice
Marks: 95.50

Use strcpy() from <string.h> to assign string values to char arrays.


3. Declaring Structure Variables

struct Student s1, s2, s3;
  • You can also declare and define together using typedef (simpler):

typedef struct {
int id;
char name[50];
float marks;
} Student;

Student s1, s2; // no need to write 'struct' every time


4. Accessing Structure Members

  • Use the dot . operator for normal variables:

s1.id = 101;
printf("%d", s1.marks);
  • For pointers to structures, use arrow -> operator:

Student *ptr = &s1;
ptr->marks = 98.0;

5. Example: Structure Pointer

#include <stdio.h>

typedef struct {
int x;
int y;
} Point;

int main() {
Point p1 = {10, 20};
Point *ptr = &p1;

printf("x = %d, y = %d\n", ptr->x, ptr->y);

return 0;
}

Output:

x = 10, y = 20

6. Nested Structures

  • Structures can contain other structures:

#include <stdio.h>

struct Date {
int day, month, year;
};

struct Student {
char name[50];
struct Date dob; // nested structure
};

int main() {
struct Student s1;
strcpy(s1.name, "Alice");
s1.dob.day = 15;
s1.dob.month = 8;
s1.dob.year = 2005;

printf("Name: %s\n", s1.name);
printf("DOB: %d-%d-%d\n", s1.dob.day, s1.dob.month, s1.dob.year);

return 0;
}

Output:

Name: Alice
DOB: 15-8-2005

7. Array of Structures

struct Student {
int id;
char name[50];
float marks;
};

int main() {
struct Student students[2];

students[0].id = 101;
strcpy(students[0].name, "Alice");
students[0].marks = 95.5;

students[1].id = 102;
strcpy(students[1].name, "Bob");
students[1].marks = 89.0;

for(int i=0; i<2; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n",
students[i].id, students[i].name, students[i].marks);
}

return 0;
}

Output:

ID: 101, Name: Alice, Marks: 95.50
ID: 102, Name: Bob, Marks: 89.00

8. Key Points About Structures

  1. Structures group different data types into a single entity.

  2. Access members using dot . (normal) or arrow -> (pointer).

  3. Use typedef to simplify structure names.

  4. Can have nested structures and arrays of structures.

  5. Structures are commonly used in records, objects, and complex data handling.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *