SlideShare a Scribd company logo
1 of 12
Structures
• A structure is a collection of one or more variables types
  grouped under a single name for easy manipulation.

• The variables in a structure, unlike those in an array, can
  be of different variable types.

• A structure can contain any of C's data types, including
  arrays and other structures.

• Each variable within a structure is called a member of
  the structure.
Using a struct
• By defining a structure you create a new data
  type.
• Once a struct is defined, you can create
  variables of the new type.

           Student stu_variable;
Accessing Structure Members
• Structure members are accessed using the
  structure member operator (.), also called the dot
  operator, between the structure name and the
  member name.
• Thus variable s1 can be access in following way
      S1.no=10;
      S1.name=”AMAR”;
      S1.per=80;

• printf("%d,%s,%f", s1.no, s1.name,s1.per);
#include <stdio.h>
struct student
{
    int id;
    char *name;
    float per;
};
void main()
{
   struct student st;
   st.id=1; st.name=“ram”; st.per=90.5;
}
Advantage Of Using structure
• One major advantage is that you can copy
  information between structures of the same type
  with a simple equation statement.
• Continuing with the preceding example, the
  statement
      S2 = S1;
  is equivalent to this statement:
      S2.no=S1.no;
      S2.name=S1.name;
      S2.per=S1.per;
Arrays of Structures
#include <stdio.h>
struct entry
{
    char fname[20];
};
main()
 { struct entry list[4];
     for (i = 0; i < 4; i++)
     {
            printf("nEnter first name: ");
            scanf("%s", list[i].fname);
     }
     for (i = 0; i < 4; i++)
     {
            printf("Name: %s ", list[i].fname,}
     }
Pointer To Structure
struct emp
{ int empno;
   char nm[20]; };
void main()
{
   struct emp e1={10,”RAJ”};
   struct emp *p;
   p=&e1;
   printf(“%d %s ”, e1.empno, e1.nm);
   printf(“%d %s ”, p->empno, p->nm);
}
UNIONS
• A union is a variable which may hold members of
  different sizes and types.
• The syntax for declaring a union is similar to that of
  a structure:
  union number
  {
      int number;
      double floatnumber;
  } anumber;
• This defines a union called "number" and a
  variable of it called "anumber".
• Members of the union can be accessed in the
  following way:
• printf("%f",anumber.floatnumber);

• When the C compiler is allocating memory for
  unions it will always reserve enough room for
  the largest member.

More Related Content

What's hot

20120810 tsql tips
20120810 tsql tips20120810 tsql tips
20120810 tsql tips
LearningTech
 

What's hot (13)

SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2SQL Fundamentals - Lecture 2
SQL Fundamentals - Lecture 2
 
Learn C# at ASIT
Learn C# at ASITLearn C# at ASIT
Learn C# at ASIT
 
Sql tables
Sql tablesSql tables
Sql tables
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Mandatory sql functions for beginners
Mandatory sql functions for beginnersMandatory sql functions for beginners
Mandatory sql functions for beginners
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
Introduction to MySQL in PHP
Introduction to MySQL in PHPIntroduction to MySQL in PHP
Introduction to MySQL in PHP
 
20120810 tsql tips
20120810 tsql tips20120810 tsql tips
20120810 tsql tips
 
Sql queries - Basics
Sql queries - BasicsSql queries - Basics
Sql queries - Basics
 
Array
ArrayArray
Array
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
Array
ArrayArray
Array
 
Msql query
Msql queryMsql query
Msql query
 

Similar to Session 6

data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 

Similar to Session 6 (20)

1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
structures.ppt
structures.pptstructures.ppt
structures.ppt
 
Introduction to structures in c lang.ppt
Introduction to structures in c lang.pptIntroduction to structures in c lang.ppt
Introduction to structures in c lang.ppt
 
03 structures
03 structures03 structures
03 structures
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
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
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
Structures
StructuresStructures
Structures
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
Chap 10(structure and unions)
Chap 10(structure and unions)Chap 10(structure and unions)
Chap 10(structure and unions)
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit4
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Session 6

  • 1. Structures • A structure is a collection of one or more variables types grouped under a single name for easy manipulation. • The variables in a structure, unlike those in an array, can be of different variable types. • A structure can contain any of C's data types, including arrays and other structures. • Each variable within a structure is called a member of the structure.
  • 2.
  • 3.
  • 4. Using a struct • By defining a structure you create a new data type. • Once a struct is defined, you can create variables of the new type. Student stu_variable;
  • 5. Accessing Structure Members • Structure members are accessed using the structure member operator (.), also called the dot operator, between the structure name and the member name. • Thus variable s1 can be access in following way S1.no=10; S1.name=”AMAR”; S1.per=80; • printf("%d,%s,%f", s1.no, s1.name,s1.per);
  • 6. #include <stdio.h> struct student { int id; char *name; float per; }; void main() { struct student st; st.id=1; st.name=“ram”; st.per=90.5; }
  • 7. Advantage Of Using structure • One major advantage is that you can copy information between structures of the same type with a simple equation statement. • Continuing with the preceding example, the statement S2 = S1; is equivalent to this statement: S2.no=S1.no; S2.name=S1.name; S2.per=S1.per;
  • 8. Arrays of Structures #include <stdio.h> struct entry { char fname[20]; };
  • 9. main() { struct entry list[4]; for (i = 0; i < 4; i++) { printf("nEnter first name: "); scanf("%s", list[i].fname); } for (i = 0; i < 4; i++) { printf("Name: %s ", list[i].fname,} }
  • 10. Pointer To Structure struct emp { int empno; char nm[20]; }; void main() { struct emp e1={10,”RAJ”}; struct emp *p; p=&e1; printf(“%d %s ”, e1.empno, e1.nm); printf(“%d %s ”, p->empno, p->nm); }
  • 11. UNIONS • A union is a variable which may hold members of different sizes and types. • The syntax for declaring a union is similar to that of a structure: union number { int number; double floatnumber; } anumber;
  • 12. • This defines a union called "number" and a variable of it called "anumber". • Members of the union can be accessed in the following way: • printf("%f",anumber.floatnumber); • When the C compiler is allocating memory for unions it will always reserve enough room for the largest member.