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

Chapter 8 Structure in C

The document discusses C structures, unions, and the differences between them. It provides examples of declaring and initializing structures, accessing structure members, and comparing structures to arrays. Unions are described as storing different data types at a single memory location, while structures allocate separate memory for each member. The key differences are that structures use more memory than unions, as unions only allocate space for the largest member.

Uploaded by

nilayshah04
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views

Chapter 8 Structure in C

The document discusses C structures, unions, and the differences between them. It provides examples of declaring and initializing structures, accessing structure members, and comparing structures to arrays. Unions are described as storing different data types at a single memory location, while structures allocate separate memory for each member. The key differences are that structures use more memory than unions, as unions only allocate space for the largest member.

Uploaded by

nilayshah04
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Structure in C

Important Question
Explain Structure with example Explain Union with example. State diff between Array and Structure State diff between Structure and Union

Topic Covered
Introduction Declaration Accessing Initialization Copying & Comparing typedef and its use in Structure Declaration Nesting of Structure

Topic Covered
Arrays of Structure Initializing Arrays of Structure Arrays within Structure Structures and Pointers Structures & Functions

Introduction
Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a materials list for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called a structs.

Structure
Structure is user defined data type which is used to store heterogeneous data under unique name. A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping together pieces of related information. Keyword 'struct' is used to declare structure. A single struct would store the data for one object. An array of structs would store the data for several objects. The variables which are declared inside the structure are called as 'members of structure'.

General Form
Struct <structure_tag_name { <data_type member_name1>; <data_type member_name2>; . . . . }<structure_variable>,<structure_var iable2>..

Explanation
The structure_tag_name is the name of the structure The structure_variables are the list of variable names separated by commas Each of these structure_variable name is a structure of type structure_tag_name Each member_name declared within the braces is called a member or structure element.

Declaration
There are three different ways to declare and/or define structure
Variable structure Tagged structure Type-defined structure

Variable Structure
struct { member_list }variable_identifie; struct { int x; int y; }a;

Tagged Structure
The tag name is referred to in subsequent declarations of variables of the type so defined. Each such declaration MUST include the keyword struct AND the name of the user-defined structure type AND the variable name(s).
struct tag_name { member_list }variable_identifie; struct s1 { int x; int y; }a;

Where Declare???
The proper place for structure declarations is in the global area of the program before main() This puts them within the scope of the entire program and is mandatory if the structure is to shared by function.

Accessing
In C program for accessing the elements of structure use dot operator('.'). For accessing element you need to the structure variable. The general format is : <structure_variable>.<member_name>; It has the same precedence as () and [], which is higher than that of any unary and binary operator. Like () and [], it associates from Left to Right.

Demo

Initialization of Structure
A structure can be initialized in much the same way as any other data type. This consists of assigning some constants to the members of the structures.

Default Value
For integer and float : 0 (zero) For char and string type : \0

General Intializing Structre


struct <structure_tag_name> { <data_type member_name1>; <data_type member_name2>; . . }<structure_variable>={constant1, constant2,..> Or struct <structre_tag_name> <structure_variable> = {constant1, constant2,..}

DEMO

ARRAY VS STRUCTURE

Union
Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of stucture. Syntax of union is similar to stucture. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time. To declare union data type, 'union' keyword is used.

Declaration
Syntax: union union_name { <data-type> element 1; <data-type> element 2; <data-type> element 3; }union_variable;
Example: union techno { int comp_id; char nm; float sal; }tch;

Memory Allocation for Union

Accessing

DEMO

Output

Difference Between Union and Structure

Memory Required for Structure


There is difference in memory allocation between union and structure as suggested in above example. The amount of memory required to store a structure variables is the sum of memory size of all members.

Memory Required for Union


But, the memory required to store a union variable is the memory required for largest element of an union.

Structure Vs Union

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy