SlideShare a Scribd company logo
1 of 14
STRUCTURES IN C
STRUCTURES
struct Date {
int month;
int day;
int year;
};
• A structure is a key word that create 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.
struct Date date;​
date.month = 2;​
date.day = 4;​
date.year = 2021;​
STRUCTURE REPRESENTATION & SIZE
•sizeof(struct …) =
sum of sizeof(field) +
alignment padding
Processor- and compiler-specific
62
61 EF BE AD DE
c1 c2 i
padding
struct CharCharInt {
char c1;
char c2;
int i;
} foo;
foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i =
0xDEADBEEF;
x86 uses “little-endian” representation
TYPEDEF
• Mechanism for creating new type names
• New names are an alias for some other type
• May improve the portability and/or clarity of the
program
typedef long int64_t;
typedef struct ADate {
int month;
int day;
int year;
} Date;
int64_t i = 100000000000;
Date d = { 2, 4, 2021 };
Overload existing type
names for portability
Simplify complex type names
CONSTANTS
• Allow consistent use of the same constant throughout
the program
• Improves clarity of the program
• Reduces likelihood of simple errors
• Easier to update constants in the program
int array[10];
for (i=0; i<10; i++) {
…
}
#define SIZE 10
int array[SIZE];
for (i=0; i<SIZE; i++) {
…
}
Preprocessor directive
Constant names are
capitalized by convention
Define once,
use throughout
the program
ARRAYS OF STRUCTURES
Date birthdays[NFRIENDS];
bool
check_birthday(Date today)
{
int i;
for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))
return (true);
return (false);
}
Constant
Array declaration
Array index, then
structure field
POINTERS TO STRUCTURES
Date
create_date1(int month,
int day,
int year)
{
Date d;
d.month = month;
d.day = day;
d.year = year;
return (d);
}
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
Copies date
Pass-by-reference
Date today;
today = create_date1(2, 4, 2021);
create_date2(&today, 2, 4, 2021);
POINTERS TO STRUCTURES
void
create_date2(Date *d,
int month,
int day,
int year)
{
d->month = month;
d->day = day;
d->year = year;
}
void
fun_with_dates(void)
{
Date today;
create_date2(&today, 2, 4, 2021);
}
today.month:
today.day:
today.year:
0x1000
0x1004
0x1008
month: 2
day: 4
year: 2021
0x30A0
0x30A4
0x30A8
d: 0x1000
0x3098
2
4
2021
ABSTRACTION IN C
struct widget;
struct widget *widget_create(void);
int widget_op(struct widget *widget, int operand);
void widget_destroy(struct widget *widget);
From the #include file widget.h:
From the file widget.c:
#include “widget.h”
struct widget {
int x;
…
};
Definition is hidden!
COLLECTIONS OF BOOLS (BIT
VECTORS)
• Byte, word, ... can represent many Booleans
One per bit, e.g., 00100101 = false, false, true, ...,
true
• Bit-wise operations:
Bit-wise AND: 00100101 & 10111100 == 00100100
Bit-wise OR: 00100101 | 10111100 == 10111101
Bit-wise NOT: ~ 00100101 == 11011010
Bit-wise XOR: 00100101 ^ 10111100 == 10011001
OPERATIONS ON BIT VECTORS
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
0…00 0111
0…01 0101
Always use C’s unsigned types for bit vectors
A mask indicates which bit positions we are interested in
0…00 0101 == 0…01 0101 & 0…00 0111
important_bits = bit_vec & low_three_bits_mask;
Selecting bits:
Result = ?
OPERATIONS ON BIT
VECTORS
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
0…00 0111
0…01 0101
bit_vec |= low_three_bits_mask;
Setting bits:
0…01 0111 == 0…01 0101 | 0…00 0111
OPERATIONS ON BIT VECTORS
const unsigned int low_three_bits_mask = 0x7;
unsigned int bit_vec = 0x15;
0…00 0111
0…01 0101
bit_vec &=
~low_three_bits_mask;
Clearing bits:
0…01 0000 == 0…01 0101 & ~0…00 0111
BIT-FIELD STRUCTURES
•Special syntax packs structure
values more tightly.
•Similar to bit vectors, but
arguably easier to read
•Nonetheless, bit vectors are
more commonly used.
•Padded to be an integral
number of words
•Placement is compiler-specific.
1 1 0 1 1 0 … …
f1 f2 f3
struct Flags {
int f1:3;
unsigned int f2:1;
unsigned int f3:2;
} my_flags;
my_flags.f1 = -2;
my_flags.f2 = 1;
my_flags.f3 = 2;

More Related Content

Similar to cprogramming Structures.pptx

C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfHimanshuKansal22
 
Javaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptxJavaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptxsanjutoppo93
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_typesNico Ludwig
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfjaipur2
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Unionnikshaikh786
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfankit11134
 
Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Pritam Samanta
 
Coding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptxCoding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptxhappycocoman
 
Using C# with U-SQL (SQLBits 2016)
Using C# with U-SQL (SQLBits 2016)Using C# with U-SQL (SQLBits 2016)
Using C# with U-SQL (SQLBits 2016)Michael Rys
 
nsider the following Date class declaration public class Da.pdf
nsider the following Date class declaration public class Da.pdfnsider the following Date class declaration public class Da.pdf
nsider the following Date class declaration public class Da.pdfadvancethchnologies
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation Hemantha Kulathilake
 
JAVA - VTU - Object oriented Concepts - 18CS45 - Module I
JAVA - VTU - Object oriented Concepts - 18CS45 - Module IJAVA - VTU - Object oriented Concepts - 18CS45 - Module I
JAVA - VTU - Object oriented Concepts - 18CS45 - Module IDemian Antony DMello
 

Similar to cprogramming Structures.pptx (20)

C programming session 13
C programming session 13C programming session 13
C programming session 13
 
04 struct-union
04 struct-union04 struct-union
04 struct-union
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
Week2a.pptx
Week2a.pptxWeek2a.pptx
Week2a.pptx
 
C language.pptx
C language.pptxC language.pptx
C language.pptx
 
slideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdfslideset 7 structure and union (1).pdf
slideset 7 structure and union (1).pdf
 
Javaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptxJavaadvance applet and applet life cycle.pptx
Javaadvance applet and applet life cycle.pptx
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types(1) cpp abstractions user_defined_types
(1) cpp abstractions user_defined_types
 
C++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdfC++ Please I am posting the fifth time and hoping to get th.pdf
C++ Please I am posting the fifth time and hoping to get th.pdf
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Please I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdfPlease I am posting the fifth time and hoping to get this r.pdf
Please I am posting the fifth time and hoping to get this r.pdf
 
Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game Computer Project For Class XII Topic - The Snake Game
Computer Project For Class XII Topic - The Snake Game
 
Coding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptxCoding - L30-L31-Array of structures.pptx
Coding - L30-L31-Array of structures.pptx
 
Using C# with U-SQL (SQLBits 2016)
Using C# with U-SQL (SQLBits 2016)Using C# with U-SQL (SQLBits 2016)
Using C# with U-SQL (SQLBits 2016)
 
nsider the following Date class declaration public class Da.pdf
nsider the following Date class declaration public class Da.pdfnsider the following Date class declaration public class Da.pdf
nsider the following Date class declaration public class Da.pdf
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation
 
ELAVARASAN.pdf
ELAVARASAN.pdfELAVARASAN.pdf
ELAVARASAN.pdf
 
JAVA - VTU - Object oriented Concepts - 18CS45 - Module I
JAVA - VTU - Object oriented Concepts - 18CS45 - Module IJAVA - VTU - Object oriented Concepts - 18CS45 - Module I
JAVA - VTU - Object oriented Concepts - 18CS45 - Module I
 

More from LECO9

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.pptLECO9
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptxLECO9
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptxLECO9
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptxLECO9
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptxLECO9
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxLECO9
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxLECO9
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptxLECO9
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptxLECO9
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptxLECO9
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxLECO9
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxLECO9
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptxLECO9
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxLECO9
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptxLECO9
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptxLECO9
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptxLECO9
 

More from LECO9 (20)

C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
Embedded Systems.pptx
Embedded Systems.pptxEmbedded Systems.pptx
Embedded Systems.pptx
 
Basic Electronics.pptx
Basic Electronics.pptxBasic Electronics.pptx
Basic Electronics.pptx
 
Intro to Microcontroller.pptx
Intro to Microcontroller.pptxIntro to Microcontroller.pptx
Intro to Microcontroller.pptx
 
PIC_Intro.pptx
PIC_Intro.pptxPIC_Intro.pptx
PIC_Intro.pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 

Recently uploaded

main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 

Recently uploaded (20)

main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 

cprogramming Structures.pptx

  • 2. STRUCTURES struct Date { int month; int day; int year; }; • A structure is a key word that create 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. struct Date date;​ date.month = 2;​ date.day = 4;​ date.year = 2021;​
  • 3. STRUCTURE REPRESENTATION & SIZE •sizeof(struct …) = sum of sizeof(field) + alignment padding Processor- and compiler-specific 62 61 EF BE AD DE c1 c2 i padding struct CharCharInt { char c1; char c2; int i; } foo; foo.c1 = ’a’; foo.c2 = ’b’; foo.i = 0xDEADBEEF; x86 uses “little-endian” representation
  • 4. TYPEDEF • Mechanism for creating new type names • New names are an alias for some other type • May improve the portability and/or clarity of the program typedef long int64_t; typedef struct ADate { int month; int day; int year; } Date; int64_t i = 100000000000; Date d = { 2, 4, 2021 }; Overload existing type names for portability Simplify complex type names
  • 5. CONSTANTS • Allow consistent use of the same constant throughout the program • Improves clarity of the program • Reduces likelihood of simple errors • Easier to update constants in the program int array[10]; for (i=0; i<10; i++) { … } #define SIZE 10 int array[SIZE]; for (i=0; i<SIZE; i++) { … } Preprocessor directive Constant names are capitalized by convention Define once, use throughout the program
  • 6. ARRAYS OF STRUCTURES Date birthdays[NFRIENDS]; bool check_birthday(Date today) { int i; for (i = 0; i < NFRIENDS; i++) { if ((today.month == birthdays[i].month) && (today.day == birthdays[i].day)) return (true); return (false); } Constant Array declaration Array index, then structure field
  • 7. POINTERS TO STRUCTURES Date create_date1(int month, int day, int year) { Date d; d.month = month; d.day = day; d.year = year; return (d); } void create_date2(Date *d, int month, int day, int year) { d->month = month; d->day = day; d->year = year; } Copies date Pass-by-reference Date today; today = create_date1(2, 4, 2021); create_date2(&today, 2, 4, 2021);
  • 8. POINTERS TO STRUCTURES void create_date2(Date *d, int month, int day, int year) { d->month = month; d->day = day; d->year = year; } void fun_with_dates(void) { Date today; create_date2(&today, 2, 4, 2021); } today.month: today.day: today.year: 0x1000 0x1004 0x1008 month: 2 day: 4 year: 2021 0x30A0 0x30A4 0x30A8 d: 0x1000 0x3098 2 4 2021
  • 9. ABSTRACTION IN C struct widget; struct widget *widget_create(void); int widget_op(struct widget *widget, int operand); void widget_destroy(struct widget *widget); From the #include file widget.h: From the file widget.c: #include “widget.h” struct widget { int x; … }; Definition is hidden!
  • 10. COLLECTIONS OF BOOLS (BIT VECTORS) • Byte, word, ... can represent many Booleans One per bit, e.g., 00100101 = false, false, true, ..., true • Bit-wise operations: Bit-wise AND: 00100101 & 10111100 == 00100100 Bit-wise OR: 00100101 | 10111100 == 10111101 Bit-wise NOT: ~ 00100101 == 11011010 Bit-wise XOR: 00100101 ^ 10111100 == 10011001
  • 11. OPERATIONS ON BIT VECTORS const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0…00 0111 0…01 0101 Always use C’s unsigned types for bit vectors A mask indicates which bit positions we are interested in 0…00 0101 == 0…01 0101 & 0…00 0111 important_bits = bit_vec & low_three_bits_mask; Selecting bits: Result = ?
  • 12. OPERATIONS ON BIT VECTORS const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0…00 0111 0…01 0101 bit_vec |= low_three_bits_mask; Setting bits: 0…01 0111 == 0…01 0101 | 0…00 0111
  • 13. OPERATIONS ON BIT VECTORS const unsigned int low_three_bits_mask = 0x7; unsigned int bit_vec = 0x15; 0…00 0111 0…01 0101 bit_vec &= ~low_three_bits_mask; Clearing bits: 0…01 0000 == 0…01 0101 & ~0…00 0111
  • 14. BIT-FIELD STRUCTURES •Special syntax packs structure values more tightly. •Similar to bit vectors, but arguably easier to read •Nonetheless, bit vectors are more commonly used. •Padded to be an integral number of words •Placement is compiler-specific. 1 1 0 1 1 0 … … f1 f2 f3 struct Flags { int f1:3; unsigned int f2:1; unsigned int f3:2; } my_flags; my_flags.f1 = -2; my_flags.f2 = 1; my_flags.f3 = 2;