SlideShare a Scribd company logo
1 of 9
Structure:
A structure is a collection of one or more variables of different data types. grouped
together under a single name.
Declaration:
The declaration of structure is:
struct struct_name
{
type variable1;
type variable2;
----------------
}
Where “struct” is the keyword to create a structure variables tag-name identifies
the particular structure and its type.
The declaration has not declared any variables .It simply describes a format called
“template “ to represent information as shown
struct book
{
char title[20];
char name[15];
int pages;
float price;
};
The structure declaration is enclosed with in a pair of curly braces.
The fields are called structure elements or members
Eg:
struct stu
{
char name[20];
int rno;
}
struct stu s={“jhon”,43};
Here the name john is assigned to s.name and 43 to s.rno.
The period() sign is used to accesss the structure members.
The link between member and a variable is established using the member operator
. Which is known as dotor period operator.
Eg:s.sname
struct stu s={“aa”,2};
struct stu s1={“bb”,3};
struct s2={“cc”,4};
WAP to accept the day month and year and display it by using structures
void main()
{
struct date
{
int day,month,year;
};
struct date d;
printf(“enter day,month,year”);
scanf(“%d%d%d”,&d.day,&d.month,&d.year);
printf(“day=%d,month=%d,year=%d”,d.day,d.month,d.year);
getch();
}
Output: 01 02 2008
Day 1 month 02 year 2008
WAP to initialize the members of a structure student as roll number,name,weight
display the content
void main ()
{
int no;
int height, weight;
char name [20];
};
structure student s={3,167,65,”aa”};
printf(“%d%d%d%s”,s.rno,s.height,s.weight,s.name};
getch();
}
Output:
3 167 65 aa
Structure with in a structure:
We can take any data type for declaring structure members like int, float ,char etc.
In the same way we can also take object of one structure as member in another
structure.
Thus,structure with in structure can be used to create complex data applications.
The syntax of the structure within the structure is
struct time
{
int second;
int minute;
int hour;
};
struct t
{
int num;
struct time st;
struct time ct;
};
WAP structure with in a structure
Array of structures
As we know array is a collection of similar data types.In the same way we can also
define array of structures.
In such type of array every element is of structure type array of structure can be
declared as
struct time
{
int second;
int minute;
int hour;
}t[3];
In this t[3] is an array of 3 elemrnts containing 3 objects or time structure . Each
element of t[3] has structure of time with 3 members that are second ,minute and
hour.
Ex prg
Array within structures:
C permits the use of arrays as structure members We can use single or multi
dimensional arrays of type int or float.
Ex: struct marks
{
int number;
flaot subject[3];
}
student[2];
Here the member subject contains 3 elements subject[0],subject[1],subject[2];
These elements can be accessed using appropriate subscripts.
Eg
Pointer to structure:
Pointer is a variable that holds the address of another data variable. The variable
may be of any data type i.e int,float or double. Starting address of the member
variable can be accessed byusing structure pointers.
struct book
{
char name[25];
char author[25];
int pages;
};
struct book*ptr;
*ptr is a pointer to structure book.Thesyntax for using pointer with member is as
given
Ptr->name
Structure and fuctions:
User defined datatypes:
There are two user defined datatypes
1.Enumerated datatype
2. Typedef
Enum:
Enum is a keyword .It is used for declaring enumeration types.
- The programmer can create his own datatype and define what values the
variables of these datatypes can hold.
- This enumeration datatype helps in reading the program.
EG: enum month(jan,feb,mar,april,may}
This statement creates a user defined datatype the keyword enum is followed by
the tagname month .enumerators jan,feb,…. And so on are
Identifiers Jan refer to 0,feb to 1 and so on.
The identifiers are not to be enclosed within quotation marks.
-Integer constants are not permitted.
Eg
Type def:
C provides the type def keyword that allows you to specify a new name for a
datatype already provided in the C programming language.
So the typedef keyword to specify an identifier for an existing datatype.
The declaration for the typedef keyword is:
Typedef datatype new name
This datatype represents the existing datatype for which you want to specify an
identifier and new name refer to that identifier.
Ex: typedef int num;
WAP for typedef
int main(void)
{
typedef int num;
num a,b,add;
a=10;
b=20;
add=0;
add=a+b;
clrscr();
printf(“the sum of %d and %d is %d”,a,b,add);
getch();
return 0;
}

More Related Content

What's hot

What's hot (20)

Structure in C
Structure in CStructure in C
Structure in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Typedef
TypedefTypedef
Typedef
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Structure c
Structure cStructure c
Structure c
 
Structures
StructuresStructures
Structures
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structures in c programming
Structures in c programmingStructures in c programming
Structures in c programming
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structure in c
Structure in cStructure in c
Structure in c
 
I1
I1I1
I1
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
#Jai c presentation
#Jai c presentation#Jai c presentation
#Jai c presentation
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 

Similar to Str

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
9.structure & union
9.structure & union9.structure & union
9.structure & unionShankar Gangaju
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)mrecedu
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languageTanmay Modi
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptxMehul Desai
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And UnionsDhrumil Patel
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RERajeshkumar Reddy
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation ashu awais
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.pptParinayWadhwa
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structuresKrishna Nanda
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Structures
StructuresStructures
Structuresselvapon
 
Definition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxDefinition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxAnithaTAssistantProf
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 

Similar to Str (20)

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Unit 1_ADC.pptx
Unit 1_ADC.pptxUnit 1_ADC.pptx
Unit 1_ADC.pptx
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Structure & union
Structure & unionStructure & union
Structure & union
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structure In C
Structure In CStructure In C
Structure In C
 
Structures
StructuresStructures
Structures
 
Definition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxDefinition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptx
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 

More from Acad

routing alg.pptx
routing alg.pptxrouting alg.pptx
routing alg.pptxAcad
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptxAcad
 
Computer Science basics
Computer Science basics Computer Science basics
Computer Science basics Acad
 
Union
UnionUnion
UnionAcad
 
Stacks
StacksStacks
StacksAcad
 
Functions
FunctionsFunctions
FunctionsAcad
 
File
FileFile
FileAcad
 
Ds
DsDs
DsAcad
 
Dma
DmaDma
DmaAcad
 
Botnet detection by Imitation method
Botnet detection  by Imitation methodBotnet detection  by Imitation method
Botnet detection by Imitation methodAcad
 
Bot net detection by using ssl encryption
Bot net detection by using ssl encryptionBot net detection by using ssl encryption
Bot net detection by using ssl encryptionAcad
 
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...Acad
 
Literature survey on peer to peer botnets
Literature survey on peer to peer botnetsLiterature survey on peer to peer botnets
Literature survey on peer to peer botnetsAcad
 
Tiny os
Tiny osTiny os
Tiny osAcad
 
Data retrieval in sensor networks
Data retrieval in sensor networksData retrieval in sensor networks
Data retrieval in sensor networksAcad
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data StruturesAcad
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysisAcad
 
Classification and prediction
Classification and predictionClassification and prediction
Classification and predictionAcad
 
Association rule mining
Association rule miningAssociation rule mining
Association rule miningAcad
 
Memory Organization
Memory OrganizationMemory Organization
Memory OrganizationAcad
 

More from Acad (20)

routing alg.pptx
routing alg.pptxrouting alg.pptx
routing alg.pptx
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptx
 
Computer Science basics
Computer Science basics Computer Science basics
Computer Science basics
 
Union
UnionUnion
Union
 
Stacks
StacksStacks
Stacks
 
Functions
FunctionsFunctions
Functions
 
File
FileFile
File
 
Ds
DsDs
Ds
 
Dma
DmaDma
Dma
 
Botnet detection by Imitation method
Botnet detection  by Imitation methodBotnet detection  by Imitation method
Botnet detection by Imitation method
 
Bot net detection by using ssl encryption
Bot net detection by using ssl encryptionBot net detection by using ssl encryption
Bot net detection by using ssl encryption
 
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
 
Literature survey on peer to peer botnets
Literature survey on peer to peer botnetsLiterature survey on peer to peer botnets
Literature survey on peer to peer botnets
 
Tiny os
Tiny osTiny os
Tiny os
 
Data retrieval in sensor networks
Data retrieval in sensor networksData retrieval in sensor networks
Data retrieval in sensor networks
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysis
 
Classification and prediction
Classification and predictionClassification and prediction
Classification and prediction
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

Str

  • 1. Structure: A structure is a collection of one or more variables of different data types. grouped together under a single name. Declaration: The declaration of structure is: struct struct_name { type variable1; type variable2; ---------------- } Where “struct” is the keyword to create a structure variables tag-name identifies the particular structure and its type. The declaration has not declared any variables .It simply describes a format called “template “ to represent information as shown struct book { char title[20]; char name[15]; int pages;
  • 2. float price; }; The structure declaration is enclosed with in a pair of curly braces. The fields are called structure elements or members Eg: struct stu { char name[20]; int rno; } struct stu s={“jhon”,43}; Here the name john is assigned to s.name and 43 to s.rno. The period() sign is used to accesss the structure members. The link between member and a variable is established using the member operator . Which is known as dotor period operator. Eg:s.sname struct stu s={“aa”,2}; struct stu s1={“bb”,3}; struct s2={“cc”,4}; WAP to accept the day month and year and display it by using structures void main()
  • 3. { struct date { int day,month,year; }; struct date d; printf(“enter day,month,year”); scanf(“%d%d%d”,&d.day,&d.month,&d.year); printf(“day=%d,month=%d,year=%d”,d.day,d.month,d.year); getch(); } Output: 01 02 2008 Day 1 month 02 year 2008 WAP to initialize the members of a structure student as roll number,name,weight display the content void main () { int no; int height, weight; char name [20]; }; structure student s={3,167,65,”aa”};
  • 4. printf(“%d%d%d%s”,s.rno,s.height,s.weight,s.name}; getch(); } Output: 3 167 65 aa Structure with in a structure: We can take any data type for declaring structure members like int, float ,char etc. In the same way we can also take object of one structure as member in another structure. Thus,structure with in structure can be used to create complex data applications. The syntax of the structure within the structure is struct time { int second; int minute; int hour; }; struct t { int num;
  • 5. struct time st; struct time ct; }; WAP structure with in a structure Array of structures As we know array is a collection of similar data types.In the same way we can also define array of structures. In such type of array every element is of structure type array of structure can be declared as struct time { int second; int minute; int hour; }t[3]; In this t[3] is an array of 3 elemrnts containing 3 objects or time structure . Each element of t[3] has structure of time with 3 members that are second ,minute and hour. Ex prg Array within structures:
  • 6. C permits the use of arrays as structure members We can use single or multi dimensional arrays of type int or float. Ex: struct marks { int number; flaot subject[3]; } student[2]; Here the member subject contains 3 elements subject[0],subject[1],subject[2]; These elements can be accessed using appropriate subscripts. Eg Pointer to structure: Pointer is a variable that holds the address of another data variable. The variable may be of any data type i.e int,float or double. Starting address of the member variable can be accessed byusing structure pointers. struct book { char name[25]; char author[25]; int pages; }; struct book*ptr;
  • 7. *ptr is a pointer to structure book.Thesyntax for using pointer with member is as given Ptr->name Structure and fuctions: User defined datatypes: There are two user defined datatypes 1.Enumerated datatype 2. Typedef Enum: Enum is a keyword .It is used for declaring enumeration types. - The programmer can create his own datatype and define what values the variables of these datatypes can hold. - This enumeration datatype helps in reading the program. EG: enum month(jan,feb,mar,april,may} This statement creates a user defined datatype the keyword enum is followed by the tagname month .enumerators jan,feb,…. And so on are Identifiers Jan refer to 0,feb to 1 and so on. The identifiers are not to be enclosed within quotation marks. -Integer constants are not permitted. Eg
  • 8. Type def: C provides the type def keyword that allows you to specify a new name for a datatype already provided in the C programming language. So the typedef keyword to specify an identifier for an existing datatype. The declaration for the typedef keyword is: Typedef datatype new name This datatype represents the existing datatype for which you want to specify an identifier and new name refer to that identifier. Ex: typedef int num; WAP for typedef int main(void) { typedef int num; num a,b,add; a=10; b=20; add=0; add=a+b; clrscr(); printf(“the sum of %d and %d is %d”,a,b,add);