SlideShare a Scribd company logo
4
Most read
9
Most read
11
Most read
Structures in C
Department of Computer Science and
Engineering
Creation of structure variable
• Definition structure.
• Declaration structure variable and
memory allocation.
Structure Definitions
• struct information
– A struct cannot contain an instance of itself.
– Can contain a member that is a pointer to the same
structure type.
– A structure definition does not reserve space in memory
• Instead creates a new data type used to declare structure
variables.
• Declarations
– Declared like other variables:
Student oneStud, Stud[66], *SPtr;
– Can use a comma separated list:
struct Student {
char name [20];
char address [20];
}oneStud, Stud[66], *SPtr;
Uses of Structures
• Data base management
• Changing the size of the cursor
• Clearing the contents of the screen
• Placing the cursor at an appropriate position on screen
• Drawing any graphics shape on the screen
• Receiving a key from the keyboard
• Checking the memory size of the computer
• Finding out the list of equipment attached to the computer
• Formatting a floppy
• Hiding a file from the directory
• Displaying the directory of a disk
• Sending the output to printer
• Interacting with the mouse
Syntax Structure definition
• struct <structure name>
{
structure element 1 ;
structure element 2 ;
structure element 3 ;
......
......
} ;
Structure Definitions
• Example
struct Student {
char name [20];
char address [20];
int rollno;
char class[5];
char year[20];
};
– struct introduces the definition for structure
Student
– Student is the structure name and is used to
declare variables of the structure type
Declaration of Struct variable
• struct book
{
char name ;
float price ;
int pages ;
} b1, b2, b3 ;
Strut
{
char name ;
float price ;
int pages ;
} b1, b2, b3 ;
struct book
{
char name ;
float price ;
int pages ;
} ;
struct book
b1, b2, b3
Assignment statements
– Example:
Student S2 = S1;
– Could also declare and initialize threeHearts as follows:
Student S2; S2.name = “Ashish”;
S2.address = “Pune”; S2.rollno = 2543;
S2.class = “SEV”; s2.year=“ 2012-1013”;
Accessing Members of Structures
• Accessing structure members
– Dot operator (.) used with structure variables
Studnt S1;
printf( "%s", S1.name );
– Arrow operator (->) used with pointers to
structure variables
Studnt *SPtr = &S1;
printf( "%s", SPtr->name );
– SPtr->name is equivalent to
( *SPtr ).name;
Memory allocation
/* Memory map of structure
elements */
main( )
{
struct book
{
char name ;
float price ;
int pages ;
} ;
struct book b1 = { 'B', 130.00, 550
} ;
printf ( "nAddress of name =
%u", &b1.name )
printf ( "nAddress of price = %u",
&b1.price ) ;
printf ( "nAddress of pages =
%u", &b1.pages ) ;
}
Here is the output of the
program...
Address of name = 65518
Address of price = 65519
Address of pages =65523
Structures in C.pptx
Structure and Assignment operator
• The values of a structure variable can be assigned to
another structure variable of the same type using the
assignment operator.
• No pease meal copy is needed .
struct employee
{
char name[10] ;
int age ;
float salary ;
} ;
Structure and Assignment operator
struct employee e1 = { "Sanjay", 30, 5500.50 } ;
struct employee e2, e3 ;
/* piece-meal copying */
strcpy ( e2.name, e1.name ) ;
e2.age = e1.age ;
e2.salary = e1.salary ;
/* copying all elements at one go */
e3 = e2 ;
printf ( "n%s %d %f", e1.name, e1.age, e1.salary ) ;
printf ( "n%s %d %f", e2.name, e2.age, e2.salary ) ;
printf ( "n%s %d %f", e3.name, e3.age, e3.salary ) ;
Typedef Example
typedef struct
{
char *first;
char *last;
char SSN[9];
float gpa;
char **classes;
} student;
student stud_a, stud[20] ,*sptr;
Point out the errors, if any, in the following programs:
main( ) {
struct
{
char name[25] ;
char language[10] ;
} ;
struct employee e = { "Hacker", "C" } ;
printf ( "n%s %d", e.name, e.language ) ;
}
What would be the output of the following
programs
struct gospel {
int num ;
char mess1[50] ;
char mess2[50] ;
} m1 = { 2, "If you are driven by
success", "make sure that it is
a quality drive“ } ;
main( )
{
struct gospel m2, m3 ;
m2 = m1 ;
m3 = m2 ;
printf ( "n%d %s %s", m1.num,
m2.mess1, m3.mess2 ) ;
}
display ( struct book *b )
{
printf ( "n%s %s %d", b->
name, b->author, b->callno
) ;
}
And here is the output...
Let us C YPK 101
Call by reference and structure
/* Passing address of a structure variable */
struct book
{
char name[25] ;
char author[25] ;
int callno ;
} ;
void main()
{
struct book b1 = { "Let us C", "YPK", 101 } ;
display ( &b1 ); }
Any question?
THANKS ALL

More Related Content

PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PPT
structure.ppt
PPTX
Programming for problem solving-II(UNIT-2).pptx
PPTX
Structure & union
PPTX
Structures_Final_KLE (2).pptx
PDF
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
DOC
Unit 5 (1)
PPT
358 33 powerpoint-slides_7-structures_chapter-7
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
structure.ppt
Programming for problem solving-II(UNIT-2).pptx
Structure & union
Structures_Final_KLE (2).pptx
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Unit 5 (1)
358 33 powerpoint-slides_7-structures_chapter-7

Similar to Structures in C.pptx (20)

PDF
637225564198396290.pdf
PDF
Pointers and Structures
PPTX
CHAPTER -4-class and structure.pptx
PPTX
detail structure presentation of problem solving
PPTX
Structures
PPTX
Fundamentals of Structure in C Programming
PPT
Structures
PPTX
CA2_CYS101_31184422012_Arvind-Shukla.pptx
PDF
VIT351 Software Development VI Unit4
PDF
1. structure
PPTX
Structure in C
PDF
Data Structure & Algorithm - Self Referential
PPTX
Address, Pointers, Arrays, and Structures2.pptx
PPTX
STRUCTURES IN C PROGRAMMING
PPTX
Structure in C language
PPTX
C structures
PPTX
12Structures.pptx
PPT
Unit4 C
PPTX
Coding - L30-L31-Array of structures.pptx
PPT
structure
637225564198396290.pdf
Pointers and Structures
CHAPTER -4-class and structure.pptx
detail structure presentation of problem solving
Structures
Fundamentals of Structure in C Programming
Structures
CA2_CYS101_31184422012_Arvind-Shukla.pptx
VIT351 Software Development VI Unit4
1. structure
Structure in C
Data Structure & Algorithm - Self Referential
Address, Pointers, Arrays, and Structures2.pptx
STRUCTURES IN C PROGRAMMING
Structure in C language
C structures
12Structures.pptx
Unit4 C
Coding - L30-L31-Array of structures.pptx
structure
Ad

More from Boni Yeamin (14)

PPTX
Enterprise Security Monitoring, And Log Management.
PPTX
Mastering LinkedIn - From Profile Setup to Networking Success
PDF
Building Active Directory Monitoring with Telegraf, InfluxDB, and Grafana
PPTX
Open source SOC Tools for Home-Lab
PPTX
security onion
PPTX
Career in Cyber Security - City University.pptx
PPTX
Effective note keeping
PPTX
Network Operations Center (NOC)
PPTX
Open Source Cybersecurity Tools
PPTX
VMware Workstation
PPTX
How to Build Your Linkedin Profile To Get Jobs.pptx
PDF
Boni Yeamin Thesis final_report.pdf
PPTX
cybersecurity analyst.pptx
PPTX
Introduction to SOC
Enterprise Security Monitoring, And Log Management.
Mastering LinkedIn - From Profile Setup to Networking Success
Building Active Directory Monitoring with Telegraf, InfluxDB, and Grafana
Open source SOC Tools for Home-Lab
security onion
Career in Cyber Security - City University.pptx
Effective note keeping
Network Operations Center (NOC)
Open Source Cybersecurity Tools
VMware Workstation
How to Build Your Linkedin Profile To Get Jobs.pptx
Boni Yeamin Thesis final_report.pdf
cybersecurity analyst.pptx
Introduction to SOC
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Presentation on HIE in infants and its manifestations
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Institutional Correction lecture only . . .
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
O7-L3 Supply Chain Operations - ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Presentation on HIE in infants and its manifestations
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharma ospi slides which help in ospi learning
Institutional Correction lecture only . . .
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
O7-L3 Supply Chain Operations - ICLT Program

Structures in C.pptx

  • 1. Structures in C Department of Computer Science and Engineering
  • 2. Creation of structure variable • Definition structure. • Declaration structure variable and memory allocation.
  • 3. Structure Definitions • struct information – A struct cannot contain an instance of itself. – Can contain a member that is a pointer to the same structure type. – A structure definition does not reserve space in memory • Instead creates a new data type used to declare structure variables. • Declarations – Declared like other variables: Student oneStud, Stud[66], *SPtr; – Can use a comma separated list: struct Student { char name [20]; char address [20]; }oneStud, Stud[66], *SPtr;
  • 4. Uses of Structures • Data base management • Changing the size of the cursor • Clearing the contents of the screen • Placing the cursor at an appropriate position on screen • Drawing any graphics shape on the screen • Receiving a key from the keyboard • Checking the memory size of the computer • Finding out the list of equipment attached to the computer • Formatting a floppy • Hiding a file from the directory • Displaying the directory of a disk • Sending the output to printer • Interacting with the mouse
  • 5. Syntax Structure definition • struct <structure name> { structure element 1 ; structure element 2 ; structure element 3 ; ...... ...... } ;
  • 6. Structure Definitions • Example struct Student { char name [20]; char address [20]; int rollno; char class[5]; char year[20]; }; – struct introduces the definition for structure Student – Student is the structure name and is used to declare variables of the structure type
  • 7. Declaration of Struct variable • struct book { char name ; float price ; int pages ; } b1, b2, b3 ; Strut { char name ; float price ; int pages ; } b1, b2, b3 ; struct book { char name ; float price ; int pages ; } ; struct book b1, b2, b3
  • 8. Assignment statements – Example: Student S2 = S1; – Could also declare and initialize threeHearts as follows: Student S2; S2.name = “Ashish”; S2.address = “Pune”; S2.rollno = 2543; S2.class = “SEV”; s2.year=“ 2012-1013”;
  • 9. Accessing Members of Structures • Accessing structure members – Dot operator (.) used with structure variables Studnt S1; printf( "%s", S1.name ); – Arrow operator (->) used with pointers to structure variables Studnt *SPtr = &S1; printf( "%s", SPtr->name ); – SPtr->name is equivalent to ( *SPtr ).name;
  • 10. Memory allocation /* Memory map of structure elements */ main( ) { struct book { char name ; float price ; int pages ; } ; struct book b1 = { 'B', 130.00, 550 } ; printf ( "nAddress of name = %u", &b1.name ) printf ( "nAddress of price = %u", &b1.price ) ; printf ( "nAddress of pages = %u", &b1.pages ) ; } Here is the output of the program... Address of name = 65518 Address of price = 65519 Address of pages =65523
  • 12. Structure and Assignment operator • The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. • No pease meal copy is needed . struct employee { char name[10] ; int age ; float salary ; } ;
  • 13. Structure and Assignment operator struct employee e1 = { "Sanjay", 30, 5500.50 } ; struct employee e2, e3 ; /* piece-meal copying */ strcpy ( e2.name, e1.name ) ; e2.age = e1.age ; e2.salary = e1.salary ; /* copying all elements at one go */ e3 = e2 ; printf ( "n%s %d %f", e1.name, e1.age, e1.salary ) ; printf ( "n%s %d %f", e2.name, e2.age, e2.salary ) ; printf ( "n%s %d %f", e3.name, e3.age, e3.salary ) ;
  • 14. Typedef Example typedef struct { char *first; char *last; char SSN[9]; float gpa; char **classes; } student; student stud_a, stud[20] ,*sptr;
  • 15. Point out the errors, if any, in the following programs: main( ) { struct { char name[25] ; char language[10] ; } ; struct employee e = { "Hacker", "C" } ; printf ( "n%s %d", e.name, e.language ) ; }
  • 16. What would be the output of the following programs struct gospel { int num ; char mess1[50] ; char mess2[50] ; } m1 = { 2, "If you are driven by success", "make sure that it is a quality drive“ } ; main( ) { struct gospel m2, m3 ; m2 = m1 ; m3 = m2 ; printf ( "n%d %s %s", m1.num, m2.mess1, m3.mess2 ) ; } display ( struct book *b ) { printf ( "n%s %s %d", b-> name, b->author, b->callno ) ; } And here is the output... Let us C YPK 101
  • 17. Call by reference and structure /* Passing address of a structure variable */ struct book { char name[25] ; char author[25] ; int callno ; } ; void main() { struct book b1 = { "Let us C", "YPK", 101 } ; display ( &b1 ); }