0% found this document useful (0 votes)
84 views16 pages

Understanding Structures and Unions in C

This document discusses C structures and unions. Structures allow the user to define a custom data type that groups together different data types. Structures can be used as function parameters and return values. Unions similarly define a custom data type, but all components in a union share the same memory space, allowing it to be interpreted in multiple ways.
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)
84 views16 pages

Understanding Structures and Unions in C

This document discusses C structures and unions. Structures allow the user to define a custom data type that groups together different data types. Structures can be used as function parameters and return values. Unions similarly define a custom data type, but all components in a union share the same memory space, allowing it to be interpreted in multiple ways.
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/ 16

Structures and Unions

Mirza Mohammad Lutfe Elahi


CSE 115 Programming Language I ECE@NSU
2

Outline
• Structure data type

• Store data for a structured object or record

• Process individual fields of a structured object

• Use structs as function parameters and return function results

• Understand the relationship between parallel arrays and arrays

of structured objects

• Union data type


CSE 115 Programming Language I ECE@NSU
3

User-defined Structure Types


• record
– a collection of information about one data object
• structure type
– a data type for a record composed of multiple
components
• hierarchical structure
– a structure containing components that are
structures

CSE 115 Programming Language I ECE@NSU


4

User-defined Structure Types

CSE 115 Programming Language I ECE@NSU


5

Individual Components of a Structured Data Object


• direct component selection operator
– a period placed between a structure type variable and a
component name to create a reference to the component
Name: Jupiter
Diameter: 142,800 km
Moons: 16
Orbit time: 11.9 years
Rotation time: 9.925 hours

CSE 115 Programming Language I ECE@NSU


6

Precedence and Associativity of Operators

CSE 115 Programming Language I ECE@NSU


7

Structure Data Type as I/O Parameters


• When a structured variable is passed as an input
argument to a function, all of its component values
are copied into the components of the function’s
corresponding formal parameter.
• When such a variable is used as an output argument,
the address-of operator must be applied in the same
way that we would pass output arguments of the
standard types char, int, and double.

CSE 115 Programming Language I ECE@NSU


8

CSE 115 Programming Language I ECE@NSU


9

CSE 115 Programming Language I ECE@NSU


10

Function Returning Structured Result


• A function that computes a structured result can be
modeled on a function computing a simple result.
• A local variable of the structure type can be allocated,
fill with the desired data, and returned as the function
result.
• The function does not return the address of the
structure as it would with an array result.
• Rather, it returns the values of all components.

CSE 115 Programming Language I ECE@NSU


11

Function Returning Structured Result

CSE 115 Programming Language I ECE@NSU


12

Function Returning Structured Result

CSE 115 Programming Language I ECE@NSU


13

Parallel Arrays and Arrays of Structures


• A natural organization of parallel arrays with data that
contain items of different types is to group the data into a
structure whose type we define.

CSE 115 Programming Language I ECE@NSU


14

Union Types
• Union - a data structure that overlays components in memory,
allowing one chunk of memory to be interpreted in multiple
ways

CSE 115 Programming Language I ECE@NSU


15

Union Types

CSE 115 Programming Language I ECE@NSU


16

Summary
• C permits the user to define a type composed of multiple
named components.
• User-defined structure types can be used in most
situations where build-in types are value.
• Structured values can be function arguments and
function results and can be copied using the assignment
operator.
• Structure types are legitimate in declarations of variables,
of structure components, and of arrays.
• In a union type, structure components are overlaid in
memory.

CSE 115 Programming Language I ECE@NSU

You might also like