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

Structure

Uploaded by

meenakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Structure

Uploaded by

meenakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

(Syllabus: Structures and unions: using structures and unions, comparison of

structure with arrays and union.)

Previous year Questions:


1. What is structure? How is it defined? How its elements are accessed? What is its
use? 7.5

2. Differentiate between structure and union by citing suitable examples? Discuss their
respective usage? 15

3. Give examples of user defined data types using a structure? What are the differences
between array and structure? 7

4. What do you mean by structures? How are structures declared and initialized? Ex-
plain with examples? How are structures different from arrays? 15

5. What do you mean by structures? How are structures declared and initialized? Ex-
plain with examples? How are structures different from unions? 15

6. What is a structure and what are its uses? 2

7. Differentiate between structure and array? 2

8. How structures differ from unions?

9. Define and distinguish between structures and unions?

Structures in C
Structures (structs):

Structure is the collection of variables of different data types (heterogeneous elements)


under a single name.

A structure is a collection of data items, usually of different data types which are com-
bined into a single unit.

A structure is a user-defined data type.


All the data item are stored in contiguous memory locations.

The keyword struct is used to create the structure.

For example: telephone directory as it consist of name, address and telephone number.

The difference between an array and a structure is that an array is a homogenous collection
of similar types, whereas a structure can have elements of different types stored adjacently
and identified by a name.

Syntax:

struct_structure_name
{
data_type_variable_name;
data_type_variable_name;
........
data_type_variable_name;
};

For Example:

struct student
{
int roll_no;
char name[20];
int age;
float marks;
};

Create a Structure
You can create a structure by using the struct keyword and declare each of
its members inside curly braces:

struct MyStructure { // Structure declaration


int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon
Structure Initialization
The initialization of a struct variable is done by placing the value of each
element inside curly brackets.

Example

struct myStructure s1 = {13, 'B', "Some text"};

Access Structure Members


To access members of a structure, use the dot syntax (.)

// Create a structure
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};

int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};

// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);

return 0;
}

Output:

13 B Some text
Difference Between Structure and Union in C
Parameter Structure Union

Keyword A user can deploy the keyword struct to de- A user can deploy the keyword union to define a
fine a Structure. Union.

Internal Implemen- The implementation of Structure in C occurs In the case of a Union, the memory allocation oc-
tation internally- because it contains separate mem- curs for only one member with the largest size
ory locations allotted to every input member. among all the input variables. It shares the same
location among all these members/objects.

Accessing Mem- A user can access individual members at a A user can access only one member at a given
bers given time. time.

Syntax The Syntax of declaring a Structure in C is: The Syntax of declaring a Union in C is:
union [union name]
struct [structure name]
{
{
type element_1;
type element_1;
type element_2;
type element_2;
.
.
.
.
} variable_1, variable_2, …;
} variable_1, variable_2, …;
Size A Structure does not have a shared location A Union does not have a separate location for ev-
for all of its members. It makes the size of a ery member in it. It makes its size equal to the
Structure to be greater than or equal to the size of the largest member among all the data
sum of the size of its data members. members.
Value Altering Altering the values of a single member does When you alter the values of a single member, it
not affect the other members of a Structure. affects the values of other members.
Storage of Value In the case of a Structure, there is a specific In the case of a Union, there is an allocation of
memory location for every input data member. only one shared memory for all the input data
Thus, it can store multiple values of the vari- members. Thus, it stores one value at a time for
ous members. all of its members.
Initialization In the case of a Structure, a user can initialize In the case of a Union, a user can only initiate the
multiple members at the same time. first member at a time.
Difference between Array and Structure
The following are the important differences between array and structure −

S.No. Array Structure


An array is a collection that
A Structure is a collection that
consists of elements of ho-
1. consists of elements of heteroge-
mogenous, i.e. of same data
neous or dissimilar data types.
type
Structure is declared using the
2. Array is declared using '[ ]'.
'struct' keyword.
Array uses subscripts or '[ ]'
Structure uses the '.' (dot opera-
3. (square brackets) to access the
tor) to access the elements.
elements
4. The size of an array is fixed The size of a structure is not fixed
Traversing through and search- Traversing through and searching
5. ing for elements in an array is for elements in a structure is slow
quick and easy. and complex.
A structure may or may not be
An array is always stored in
6. stored in contiguous memory lo-
contiguous memory locations.
cations.

Difference Between Structure and Array in C


Parameter Structure in C Array in C

Definition It is a type of data structure in the form of a It is a type of data structure that works as a
container that holds variables of different container to hold variables of the very same
types. type. Array does not support variables of mul-
tiple data types.

Allocation of In a structure, the memory allocation for The array stores the input data in a memory
Memory the input data doesn’t require being in con- allocation of contiguous type. It means that
secutive memory locations. the array stores its data in a type of memory
model where the memory blocks hold con-
secutive addresses (it assigns memory
blocks consecutively).

Accessibility For a user to access the elements present On the other hand, any user can easily ac-
in a structure, they require the name of cess the elements by index in an array’s
that particular element (it is mandatory for
retrieval). case.

Pointer A structure holds no concept of internal An array, on the other hand, implements
Pointer. Pointer internally. It always points at the very
first element present in the array.

Instantiation One can create an object from the struc- An array does not allow the creation of an ob-
ture after a later declaration in its program. ject after the declaration.

Types of Data A structure includes multiple forms of data- A user cannot have multiple forms of data-
Type Variables type variables in the form of input. type variables in an array because it supports
only the same form of data-type variables.

Performance A structure becomes very slow in perfor- The process of searching and accessing ele-
mance due to the presence of multiple ments is much faster in the case of an array
data-types. The process of searching and due to the absence of multiple data-type vari-
accessing elements becomes very slow in ables. It is, thus, better and faster in perfor-
these. mance.

Syntax struct sructure_name{ type name_of_array [size]

element type 1;

element type 2;

} variable no.1, variable no.2, . .;


Bit Field You can define a Bit field in a structure. You cannot define a Bit field in an array.
Access You can access the Structure elements by You can access the Array elements by their
their names. index numbers.
Operators The element accessing operator for a The element accessing operator and declara-
structure is a dot operator “.“ tion for an array is a square bracket [ ]
Size The various elements in a structure are of The array contains various elements of the
different sizes each. same size.
Keyword We use the keyword “struct” to define a No keyword is present to declare an array.
structure.
User-defined The structure is a user-defined form of An array isn’t user-defined. It is declared di-
data type. rectly.

You might also like