KEMBAR78
Structure in C 4 | PDF
0% found this document useful (0 votes)
4 views4 pages

Structure in C 4

A structure in C is a user-defined data type that groups variables of different types under a single name, useful for organizing complex data. Structures are declared using the 'struct' keyword, and members can be accessed using the dot operator or via pointers with the arrow operator. Unlike structures, unions share memory among their members, allowing only one value to be valid at a time.

Uploaded by

royrian149
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 views4 pages

Structure in C 4

A structure in C is a user-defined data type that groups variables of different types under a single name, useful for organizing complex data. Structures are declared using the 'struct' keyword, and members can be accessed using the dot operator or via pointers with the arrow operator. Unlike structures, unions share memory among their members, allowing only one value to be valid at a time.

Uploaded by

royrian149
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
You are on page 1/ 4

Structure in C

A structure is a user-defined data type in C that allows grouping variables of different types
under a single name. Structures are particularly useful for organizing and representing complex
data.

---

How to Declare a Structure

To define a structure:

struct StructureName {
data_type member1;
data_type member2;
// Add more members as needed
};

Example:

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

---

How to Declare and Access Structure Members

1. Declare Structure Variables:

struct StructureName variable_name;

Example:

struct Student student1;

2. Access Structure Members:

Use the dot operator (.) for direct access.


Syntax:

variable_name.member_name

Example:

student1.id = 101; strcpy(student1.name, "John");


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

3. Access Using Pointers:

Use the arrow operator (->) when accessing members via a pointer.

Syntax:

pointer_name->member_name

Example:

struct Student *ptr = &student1;


ptr->id = 102;
printf("ID: %d\n", ptr->id);

---

Difference Between Structure and Union

---

Example: Structure vs. Union

Structure Example:

#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float marks;
};

int main() {
struct Student student1;
student1.id = 101;
strcpy(student1.name, "Alice");
student1.marks = 95.5;

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


return 0;
}

Union Example:

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

union Data {
int id;
char name[50];
float marks;
};

int main() {
union Data data;
data.id = 101;
printf("ID: %d\n", data.id);

strcpy(data.name, "Alice");
printf("Name: %s\n", data.name);

data.marks = 95.5;
printf("Marks: %.2f\n", data.marks);
return 0;
}

---
Key Observations

1. In Structure:

All members can store data independently.

Output:

ID: 101, Name: Alice, Marks: 95.50

2. In Union:

Memory is shared, so only one value is valid at a time.

Output:

ID: 101
Name: Alice
Marks: 95.50

You might also like