0% found this document useful (0 votes)
2 views

unit 5

The document provides an overview of structures in C, including their definition, syntax, and various operations such as declaration, initialization, and accessing members. It also covers related concepts like nested structures, typedef, unions, self-referential structures, and dynamic memory allocation. Additionally, it highlights the differences between structures and arrays, and includes examples of structure declarations and applications.

Uploaded by

raajeevmagesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit 5

The document provides an overview of structures in C, including their definition, syntax, and various operations such as declaration, initialization, and accessing members. It also covers related concepts like nested structures, typedef, unions, self-referential structures, and dynamic memory allocation. Additionally, it highlights the differences between structures and arrays, and includes examples of structure declarations and applications.

Uploaded by

raajeevmagesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

S.

Question
No
1 What is structure? Write the syntax for structure.

A structure in C is a user-defined data type that allows grouping variables of different types under a
single name. Structures are used to represent complex data types, similar to records in other program
languages.

They are particularly useful for grouping related data, such as attributes of a person (name, age, etc.)
student (roll number, marks, etc.).

struct Student
{
char name[50]; // character array to hold student's name
int roll_no; // integer to hold roll number
float marks; // float to hold marks
};

2 Write the various operations on structure.


1. Declaration and Definition
2. Initialization
3. Accessing Structure Members
4. Structure Assignment
5. Pointer to a Structure
6. Passing Structures to Functions
7. Returning Structures from Functions
8. Nested Structures
9. Array of Structures
10. Dynamic Memory Allocation for Structure
3 How the members of structure object is accessed?
Dot (.) Operator — Used to access members of a structure when you have a structure variable.
Arrow (->) Operator — Used to access members of a structure when you have a pointer to a
structure.
Syntax:
structure_variable.member_name

4 Write the use of size operator on structure.


The sizeof operator is used to determine the size (in bytes) of a data type or a variable, including
structures. When applied to a structure, sizeof returns the total size required to store the structure
in memory, which includes the size of all its members, as well as any padding that the compiler
may add for alignment purposes.
Syntax:
sizeof(type_name)

5 What is a nested structure?


A nested structure is a structure in which one or more members are themselves structures. In other
words, a structure can contain another structure as one of its members. This allows you to model
more complex data relationships by creating hierarchical data structures.
Syntax:
struct Outer {
// Members of the outer structure
struct Inner {
// Members of the inner structure
} inner_var;
};

6 How typedef is used in structure?


ypedef is used to create an alias (a new name) for an existing data type. When used with
structures, typedef can simplify the syntax and make your code more readable and convenient by
allowing you to refer to structures using shorter names.
Syntax:
typedef struct {
// Members of the structure
} NewTypeName;
7 Interpret the term Union in C.
A union is a special data type that allows you to store different data types in the same memory
location. Unlike a structure, where each member has its own memory location, all members of a
union share the same memory. This means that at any given time, only one member of the union
can hold a value, but it can hold a value of any of the types defined in the union.

8 What is mean by Self referential structures?


A self-referential structure (or recursive structure) is a structure in C where a member of the
structure is a pointer to the same type of structure. This allows a structure to "refer to itself"
indirectly through its members, enabling the creation of linked data structures such as linked lists,
trees, and other recursive data structures.
Syntax:
struct Node {
int data; // Some data
struct Node *next; // Pointer to another node of the same type
};

9 Point out the meaning of Dynamic memory allocation.

Dynamic memory allocation refers to the process of allocating memory during the execution of a
program, rather than at compile time. This allows a program to request memory as needed and to
release it when it is no longer needed, offering flexibility in managing memory based on runtime
conditions.

In C, dynamic memory allocation is done using functions defined in the <stdlib.h> library,
specifically:

● malloc()
● calloc()
● realloc()
● free()

10 Mention any two application linked list.


1.Implementation of Dynamic Memory Allocation
2. Implementation of Queues and Stacks (in Data Structures)
11 What is the need for typedef?
In C, typedef is a keyword used to create alias names (or type definitions) for existing data types.
It allows you to define new names for data types, which can make your code more readable,
maintainable, and easier to manage.

12 Generalize the operators used in access the structure members.

Generalization of Operators Used to Access Structure Members

● Dot Operator (.): Used when you have a structure variable (non-pointer).
● Arrow Operator (->): Used when you have a pointer to a structure

13 Discover the meaning of Array of structure.


An array of structures is a collection of multiple structure variables stored in contiguous memory
locations, where each element in the array is a structure. This allows you to manage and store a
collection of similar types of data (but with different fields) efficiently. It combines the benefits of
both arrays (for handling multiple data elements) and structures (for grouping different types of
data).

Syntax:
struct StructureName arrayName[size];

14 Show the difference between Structure from Array.

Structure Array
A structure is a user-defined data type that
allows grouping together different types of An array is a collection of variables of the
data elements (variables). Each element in a same data type. All elements in an array are of
structure is called a member, and each member the same type and are stored in contiguous
can have a different data type. memory locations.

Structures are used when you need to group Arrays are used when you need to store
different types of data together to represent an multiple values of the same type, such as a
entity (like a student with name, age, and collection of integers, floats, or characters.
marks).
struct StructureName { data_type array_name[size];
data_type member1;
data_type member2;
// More members
};

15 Invent the application of size of operator to this structure.

Consider the declaration: struct


{
charname;
int num;
} student;
The sizeof operator is used to determine the size (in bytes) of a data type or a variable. The size of
a structure depends on the sizes of its members and any padding added by the compiler for
alignment purposes.

This structure is anonymous (i.e., it doesn’t have a name) and contains two members:

● char name: a single character


● int num: an integer

16 Show a structure called ID_Card to hold the details of student.

struct ID_Card {
char name[50]; // Student's name (string)
int roll_number; // Roll number of the student (integer)
char course[50]; // Course the student is enrolled in (string)
char dob[15]; // Date of birth in string format (e.g., DD/MM/YYYY)
};

int main() {
// Declare and initialize a structure variable for a student
struct ID_Card student1 = {
"John Doe", // Name
101, // Roll number
"Computer Science",// Course
"15/03/2001" // Date of birth
};

You might also like