Wednesday, January 15, 2025
HomeTechWhat is C Structures

What is C Structures

A structure in C is a user-defined data type that groups related variables of different data types into a single entity. Structures are particularly useful for representing complex data like records or objects.

Syntax of a Structure

c
struct structure_name {
data_type member1;
data_type member2;
...
data_type memberN;
};
  • struct: Keyword used to define a structure.
  • structure_name: The name of the structure.
  • member1, member2, ...: Variables (members) inside the structure.

Example: Defining and Using a Structure

Define a Structure

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

Declare and Initialize a Structure Variable

c
#include <stdio.h>

int main() {
struct Student student1; // Declare a variable of the structure

// Initialize members
student1.id = 101;
strcpy(student1.name, "John Doe"); // Copy string to name
student1.marks = 95.5;

// Access and Print Members
printf("ID: %d\n", student1.id);
printf("Name: %s\n", student1.name);
printf("Marks: %.2f\n", student1.marks);

return 0;
}

Features of Structures

  1. Grouping Data: Combines different types of variables into one entity.
  2. Access Members: Use the dot operator (.) to access structure members.
  3. Memory Allocation: Members are stored in contiguous memory.

Example with Array of Structures

You can create an array of structures to handle multiple records.

c
#include <stdio.h>

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

int main() {
struct Student students[3]; // Array of structures

// Input student details
for (int i = 0; i < 3; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}

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

return 0;
}

Nested Structures

A structure can have another structure as a member.

c
#include <stdio.h>

struct Address {
char city[30];
int zip;
};

struct Student {
int id;
char name[50];
struct Address address;
};

int main() {
struct Student student = {101, "Alice", {"New York", 10001}};

printf("ID: %d\n", student.id);
printf("Name: %s\n", student.name);
printf("City: %s\n", student.address.city);
printf("ZIP: %d\n", student.address.zip);

return 0;
}

Difference Between Structure and Array

Feature Structure Array
Data Type Can hold different data types Holds elements of the same type
Size Fixed for defined members Size depends on the number of elements
Access Dot operator for members Indexing for elements

Advantages of Structures

  1. Organized Data: Groups related variables together.
  2. Flexibility: Supports various data types.
  3. Reusability: Can be used to define complex data models.

Limitations

  1. No Member Functions: Cannot include functions directly (unlike C++ classes).
  2. No Data Hiding: Members are publicly accessible.

Structures are foundational in C for managing complex data, and they provide a stepping stone to advanced programming concepts like classes in object-oriented languages

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x