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

Structure

The document discusses structures in C programming, including defining a structure, creating structure variables, accessing structure members, and arrays of structures. Structures allow grouping different data types together into a single type. Structures are defined using the struct keyword and members are accessed using dot or arrow operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Structure

The document discusses structures in C programming, including defining a structure, creating structure variables, accessing structure members, and arrays of structures. Structures allow grouping different data types together into a single type. Structures are defined using the struct keyword and members are accessed using dot or arrow operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PROG0101 Fundamentals of Programming

structure

1
Structure
• A structure is a user defined data type in C. A structure creates
a data type that can be used to group items of possibly
different types into a single type.

Difference Between Array and Structure


An array is a collection of variables of same data type whereas
a structure is a collection of variables of different data type.
Defining a Structure
To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member.
The format of the struct statement is as follows −

The structure tag is optional and each member definition is a


normal variable definition, such as int i; or float f; or any other
valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure
variables but it is optional.
.

Example of Structure:

Here, a type struct Person is defined.


Create struct variables
• When a struct type is declared, no storage or memory is
allocated. To allocate memory of a given structure type and
work with it, we need to create variables.
Accessing Structure Members
There are two types of operators used for accessing members
of a structure.
1. . – Member Operator
2. -> - Structure pointer operator

Suppose, you want to access the salary of person2.


person2.salary;
Example:
Array of Structure
C Structure is collection of different datatypes ( variables )
which are grouped together.
Whereas, array of structures is nothing but collection of
structures. This is also called as structure array in C.
EXAMPLE PROGRAM FOR ARRAY OF
STRUCTURES IN C:

This program is used to store and access “id, name and


percentage” for 3 students.
Structure array is used in this program to store and display
records for many students. You can store “n” number of
students record by declaring structure variable as ‘struct
student record[n]“, where n can be 1000 or 5000 etc.
EXAMPLE PROGRAM FOR DECLARING MANY STRUCTURE
VARIABLE IN C:
In this program, two structure variables “record1″ and “record2″ are
declared for same structure and different values are assigned for both
structure variables. Separate memory is allocated for both structure
variables to store the data.

You might also like