STRUCTURES AND
UNIONS
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSE
Recap
2
Problem 1
• Storing the 5 subject marks for 10 students in a class
60 40 60 80 100
60 55 60 80 100
10 76 60 78 100
10 66 10 10 80
80 100 10 10 80
80 100 60 40 80
10 40 80 40 60
10 40 100 100 60
10 40 100 56 60
10 40 100 80 80
int marks[10][5]
Problem 2
• Storing the 5 subject marks for 10 students in a class
NAVJEET
SINGROHA 60 40 60 80 100 68
ABHISHEK
ANAND 60 55 60 80 100 71
ADITYA JUYAL 10 76 60 78 100 64.8
AMIT PANDEY 10 66 10 10 80 35.2
ANANDITA
SINGH 80 100 10 10 80 56
ANIRUDH
PUROHIT 80 100 60 40 80 72
ANTRIKSH
SINGH 10 40 80 40 60 46
ANUBHA TYAGI 10 40 100 100 60 62
ANUSHKA
YADAV 10 40 100 56 60 53.2
ARPIT SINGH 10 40 100 80 80 62
Definition : Structure
• Group of dissimilar Datatypes
• Defines a new type
• a new kind of data type that compiler regards as a single unit
• User Defined datatype
• Uses struct keyword to create
Example
Structure Declaration
Example – Structure Declaration
• Creates a structure datatype named motor
Structure Variable Declaration
• Creating variable of structure type
• Two ways as structure variable
• With structure declaration
• Using structure tag
• As structure pointer
Accessing Members of Structure
Method 1 : Using . operator
struct ADate {
int month;
int day;
int year;
};
struct ADate date;
date.month = 1;
date.day = 19;
date.year = 2017;
OR
Method 2 : Using ->operator
struct Date {
int month;
int day;
int year;
};
Date d1;
Date *d=&d1;
d->month = month;
d->day = day;
d->year = year;
Structures inside Structures
Example
struct COST { int amount;
char currency_type; }
struct PART { struct COST cost;
char id[2];
int num_avail;
}
Unions
• Differ in memory allocation.
• In Structure its “AND” and in Unions its “XOR”
• Accessing remains the same as structures
union sample {
float avg;
int rollno;
char *name;
};
/* either a float xor an int xor a string */
Definition
• Like Structures Unions can stores disimilar data elements
in a group.
• But you can store only one data element at a time.
• Once a new value is assigned to any member value the
older one is erased.
• Storage size of union is the size of its largest member
STORAGE CLASSES
Need???
• defines the scope (visibility)
• life time of variables
• storage classes used in a C Program
• auto
• register
• static
• extern
Auto Storage Class
• auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}
• The example above defines two variables with the same
storage class. auto can only be used within functions, i.e.
local variables.
Register Storage Class
• Used to define local variables
• Stored in Register instead of RAM
• Maximum size equal to register size
• Eg. Frequently used variables like counter
{
register int Miles;
}
Static Storage class
• provides a lifetime over the entire program,
• declared with the keyword static
• variables are automatically initialized to zero
• static int a;
• They continue to exist even after the block in which they
are defined terminates.
• storage allocated becomes permanent for the duration of
the program.
Extern Storage Class
• extern is used to give a reference of a global variable that
is visible to ALL the program files.
• extern int a;
INFO106 COMPUTER
PROGRAMMING
Summary of Units
1. Introduction
2. C Programming Basics
3. Arrays and Strings
4. Functions and Pointers
5. Structure and Unions
Future Enhancement
• Working with our own header files
• Embedded C
• Other Programming Languages
Comparison of PL
Embedded C example
cont….
CSEG1001 Unit 5 Structure and Unions
CSEG1001 Unit 5 Structure and Unions

CSEG1001 Unit 5 Structure and Unions

  • 1.
  • 2.
  • 3.
    Problem 1 • Storingthe 5 subject marks for 10 students in a class 60 40 60 80 100 60 55 60 80 100 10 76 60 78 100 10 66 10 10 80 80 100 10 10 80 80 100 60 40 80 10 40 80 40 60 10 40 100 100 60 10 40 100 56 60 10 40 100 80 80 int marks[10][5]
  • 4.
    Problem 2 • Storingthe 5 subject marks for 10 students in a class NAVJEET SINGROHA 60 40 60 80 100 68 ABHISHEK ANAND 60 55 60 80 100 71 ADITYA JUYAL 10 76 60 78 100 64.8 AMIT PANDEY 10 66 10 10 80 35.2 ANANDITA SINGH 80 100 10 10 80 56 ANIRUDH PUROHIT 80 100 60 40 80 72 ANTRIKSH SINGH 10 40 80 40 60 46 ANUBHA TYAGI 10 40 100 100 60 62 ANUSHKA YADAV 10 40 100 56 60 53.2 ARPIT SINGH 10 40 100 80 80 62
  • 5.
    Definition : Structure •Group of dissimilar Datatypes • Defines a new type • a new kind of data type that compiler regards as a single unit • User Defined datatype • Uses struct keyword to create
  • 6.
  • 7.
  • 8.
    Example – StructureDeclaration • Creates a structure datatype named motor
  • 9.
    Structure Variable Declaration •Creating variable of structure type • Two ways as structure variable • With structure declaration • Using structure tag • As structure pointer
  • 11.
  • 12.
    Method 1 :Using . operator struct ADate { int month; int day; int year; }; struct ADate date; date.month = 1; date.day = 19; date.year = 2017; OR
  • 14.
    Method 2 :Using ->operator struct Date { int month; int day; int year; }; Date d1; Date *d=&d1; d->month = month; d->day = day; d->year = year;
  • 15.
    Structures inside Structures Example structCOST { int amount; char currency_type; } struct PART { struct COST cost; char id[2]; int num_avail; }
  • 16.
    Unions • Differ inmemory allocation. • In Structure its “AND” and in Unions its “XOR” • Accessing remains the same as structures union sample { float avg; int rollno; char *name; }; /* either a float xor an int xor a string */
  • 17.
    Definition • Like StructuresUnions can stores disimilar data elements in a group. • But you can store only one data element at a time. • Once a new value is assigned to any member value the older one is erased. • Storage size of union is the size of its largest member
  • 20.
  • 21.
    Need??? • defines thescope (visibility) • life time of variables • storage classes used in a C Program • auto • register • static • extern
  • 22.
    Auto Storage Class •auto is the default storage class for all local variables. { int Count; auto int Month; } • The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.
  • 23.
    Register Storage Class •Used to define local variables • Stored in Register instead of RAM • Maximum size equal to register size • Eg. Frequently used variables like counter { register int Miles; }
  • 24.
    Static Storage class •provides a lifetime over the entire program, • declared with the keyword static • variables are automatically initialized to zero • static int a; • They continue to exist even after the block in which they are defined terminates. • storage allocated becomes permanent for the duration of the program.
  • 25.
    Extern Storage Class •extern is used to give a reference of a global variable that is visible to ALL the program files. • extern int a;
  • 26.
  • 27.
    Summary of Units 1.Introduction 2. C Programming Basics 3. Arrays and Strings 4. Functions and Pointers 5. Structure and Unions
  • 28.
    Future Enhancement • Workingwith our own header files • Embedded C • Other Programming Languages
  • 29.
  • 30.
  • 31.