0% found this document useful (0 votes)
14 views12 pages

CPPS Struct

Uploaded by

sarveshnaik8706
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)
14 views12 pages

CPPS Struct

Uploaded by

sarveshnaik8706
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

1.

Array of Structures
● What it is:
An array of structures allows you to store
multiple records of the same type (e.g.,
information about multiple students).
● Example:
struct Student {
int roll_no;
char name[50];
float marks;
};

struct Student students[3]; // Array of 3


students
● Use Case:
Managing a collection of records like a list of
employees, students, or products.

2. Pointer to Structures
● What it is:
A pointer to a structure allows you to access
and manipulate the structure's members
using the -> operator instead of the dot (.)
operator.
● Example:
struct Student student1 = {101,
"Vaishali", 95.5};
struct Student *ptr = &student1;

printf("Roll No: %d\n", ptr->roll_no); //


Access via pointer
● Use Case:
Useful when passing structures to functions
by reference or when working with
dynamically allocated memory for structures.

3. Nested Structures
● What it is:
A structure inside another structure to
represent hierarchical or related data.
● Example:
struct Address {
char city[50];
int zip;
};

struct Student {
int roll_no;
char name[50];
struct Address address; // Nested
structure
};

struct Student student1 = {101,


"Vaishali", {"Pune", 411001}};
printf("City: %s\n",
[Link]);
● Use Case:
Useful when managing complex entities, like
a student with a permanent and temporary
address.

4. Structure as Function Arguments


● What it is:
Structures can be passed to functions either:
○ By Value: A copy of the structure is
passed.
○ By Reference: A pointer to the structure
is passed to avoid copying.
● Example:
void display(struct Student s) {
printf("Name: %s\n", [Link]);
}

struct Student student1 = {101,


"Vaishali", 95.5};
display(student1); // Passing by value
● Use Case:
Useful for modularizing code and avoiding
redundant logic in a program.

5. Dynamic Memory Allocation for Structures


● What it is:
Memory for structures is allocated
dynamically using malloc or calloc when the
size or number of records isn’t known at
compile time.
● Example:
struct Student *s = (struct Student
*)malloc(sizeof(struct Student));
s->roll_no = 101;
strcpy(s->name, "Vaishali");
s->marks = 95.5;
free(s); // Free the allocated memory
● Use Case:
Efficient when dealing with large or dynamic
data sets, such as managing an unknown
number of students.

6. Typedef and Structures


● What it is:
Use typedef to simplify the usage of
structure names by creating an alias.
● Example:
typedef struct Student {
int roll_no;
char name[50];
} Student;

Student s1 = {101, "Vaishali"}; // No need


to use `struct`
● Use Case:
Simplifies code readability and reduces
verbosity.

7. Self-Referential Structures
● What it is:
A structure that contains a pointer to itself,
commonly used in linked lists, trees, and
graphs.
● Example:
struct Node {
int data;
struct Node *next; // Pointer to the next
node
};
● Use Case:
Essential for implementing data structures
like linked lists, stacks, and queues.
Advanced Use Case
Topic

Array of Managing a list of similar entities like


Structures students or employees.

Pointer to Efficient access and manipulation of


Structures structure members.

Nested Representing hierarchical


Structures relationships like address inside a
student.

Structures Modular programming by passing


as data to functions.
Arguments
Dynamic Handling data whose size is not
Allocation known at compile time.

Typedef Simplifying structure usage in large


and programs.
Structures

Self-Refere Building complex data structures like


ntial linked lists, trees, and graphs.
Structures

Comparison of Static and Dynamic Memory


Allocation:

Aspect Static Memory Dynamic Memory


Allocation Allocation

Definition Memory is allocated Memory is allocated


at compile time. at runtime.
Flexibility Fixed size; cannot be Flexible; size can be
resized during changed
execution. dynamically.

Efficiency Faster since memory Slower due to


is allocated at runtime allocation
compile time. overhead.

Control Controlled by the Controlled by the


compiler. programmer using
functions like
malloc.

Functions No special functions Requires explicit use


Used are required. of functions like
Variables are defined malloc, calloc,
normally. realloc, and free.

Lifetime Memory exists for Memory exists until


the entire lifetime of explicitly freed or the
the program. program ends.
Memory May lead to wasted Efficient use of
Usage memory if memory as it is
over-allocated. allocated only when
needed.

Scope Limited to the block Accessible anywhere


or function where the through pointers if
variable is declared. properly managed.

Example int arr[10]; // Array of int *arr = (int


Syntax fixed size 10 *)malloc(10 *
sizeof(int)); //
Dynamically
allocated array

Risk of No risk of memory High risk of memory


Memory leaks as allocation leaks if memory is
Leaks and deallocation are not explicitly freed.
automatic.
Error Compiler checks for Programmer must
Handling errors during handle errors (e.g.,
allocation. failed allocation).

Usage Suitable for small, Suitable for large,


fixed-size data dynamic, or
structures. unknown-size data
structures.

You might also like