SlideShare une entreprise Scribd logo
1  sur  8
C Data Types:
Primary data types                       Derived
Derived data types                        Types
User-defined data types



    Function                                Pointer            Structure
                     Array Type                                                  Union Type
      Type                                   Type                Type

    Array – Collection of one or more related variables of similar
               data type grouped under a single name
 Structure – Collection of one or more related variables of different
             data types, grouped under a single name

         In a Library, each book is an object, and its characteristics like title, author, no of
pages, price are grouped and represented by one record.
           The characteristics are different types and grouped under a aggregate variable of
different types.
       A record is group of fields and each field represents one characteristic. In C, a record
is implemented with a derived data type called structure. The characteristics of record are
called the members of the structure.
Book-1                      Book-2                               Book-3
BookID: 1211                BookID: 1212                         BookID: 1213
Title : C Primer Plus       Title : The ANSI C Programming       Title : C By Example
Author : Stephen Prata      Author : Dennis Ritchie              Author : Greg Perry
Pages : 984                 Pages : 214                          Pages : 498
Price : Rs. 585.00          Price : Rs. 125.00                   Price : Rs. 305.00

   book                        book_id        integer        2 bytes
                  bookid
                                 title        Array of 50 characters                    50 bytes
                    title
                                 author       Array of 40 characters            40 bytes
                  author
                                 pages        integer         2 bytes
                  pages
                   price         price        float                      4 bytes

                                         Memory occupied by a Structure variable

               STRUCTURE- BOOK                        struct < structure_tag_name >
struct book {                                         {
                            Structure tag                   data type < member 1 >
      int book_id ;
      char title[50] ;                                      data type < member 2 >
                                                               …. …. …. ….
      char author[40] ;
                                                            data type < member N >
      int pages ;                                     };
      float price ;
};
Declaring a Structure Type                   Declaring a Structure Variable

 struct student                               struct student s1,s2,s3;
 {                                                      (or)
           int roll_no;                       struct student
           char name[30];                     {
           float percentage;                            int roll_no;
 };                                                     char name[30];
                                                        float percentage;
                                              }s1,s2,s3;

      Initialization of structure                       Reading values to members at
                                                        runtime:
Initialization of structure variable while
declaration :                                           struct student s3;
   struct student s2 = { 1001, “ K.Avinash ”,           printf(“nEnter the roll no”);
                  87.25 } ;                             scanf(“%d”,&s3.roll_no);
Initialization of structure members individually :      printf(“nEnter the name”);
   s1. roll_no = 1111;                                  scanf(“%s”,s3.name);
   strcpy ( s1. name , “ B. Kishore “ ) ;               printf(“nEnter the percentage”);
   s1.percentage = 78.5 ;                               scanf(“%f”,&s3.percentage);

                      membership operator
Implementing a Structure
struct employee {
     int empid;
     char name[35];
     int age;                   Declaration of Structure Type
     float salary;
};
                                           Declaration of Structure variables
int main() {
    struct employee emp1,emp2 ;      Declaration and initialization of Structure variable

   struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ;
   emp1.empid=1211;
   strcpy(emp1.name, “K.Ravi”);
   emp1.age = 27;                         Initialization of Structure members individually
   emp1.salary=30000.00;
   printf(“Enter the details of employee 2”);      Reading values to members of Structure
   scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary);
   if(emp1.age > emp2.age)
        printf( “ Employee1 is senior than Employee2n” );
   else
        printf(“Employee1 is junior than Employee2n”);
                                                             Accessing members of Structure
  printf(“Emp ID:%dn Name:%sn Age:%dn Salary:%f”,
emp1.empid,emp1.name,emp1.age,emp1.salary);
}
Arrays And structures                               Nesting of structures
struct student                                        struct date {
{                                                         int day ;
   int sub[3] ;                                           int month ;
                                                                             Outer Structure
   int total ;                                            int year ;
};                                                    };
                                                      struct person {
                                                          char name[40];
int main( ) {                                             int age ;
  struct student s[3];                                    struct date b_day ;
  int i,j;                                            };
   for(i=0;i<3;i++) {                                 int main( ) {             Inner Structure
      printf(“nnEnter student %d marks:”,i+1);         struct person p1;
      for(j=0;j<3;j++) {                                 strcpy ( p1.name , “S. Ramesh “ ) ;
           scanf(“%d”,&s[i].sub[j]);                     p1. age = 32 ;
       }                                                 p1.b_day.day = 25 ;       Accessing Inner
    }                                                    p1.b_day. month = 8 ; Structure members
    for(i=0;i<3;i++) {                                   p1.b_day. year = 1978 ;
         s[i].total =0;                               }
         for(j=0;j<3;j++) {
               s[i].total +=s[i].sub[j];              OUTPUT:
       }                                              Enter student 1 marks: 60 60 60
       printf(“nTotal marks of student %d is: %d”,   Enter student 2 marks: 70 70 70
                          i+1,s[i].total );           Enter student 3 marks: 90 90 90
    }
}                                                     Total marks of student 1 is: 180
                                                      Total marks of student 2 is: 240
                                                      Total marks of student 3 is: 270
structures and functions                           Self referential structures
struct fraction {                      struct student_node {
    int numerator ;                        int roll_no ;
    int denominator ;                      char name [25] ;
};                                         struct student_node *next ;
                                       };
void show ( struct fraction f )        int main( )
{                                       {
  printf ( “ %d / %d “, f.numerator,      struct student_node s1 ;
                                          struct student_node s2 = { 1111, “B.Mahesh”, NULL } ;
                f.denominator ) ;         s1. roll_no = 1234 ;
}                                         strcpy ( s1.name , “P.Kiran “ ) ;
int main ( ) {                             s1. next = & s2 ;       s2 node is linked to s1 node
    struct fraction f1 = { 7, 12 } ;
    show ( f1 ) ;                          printf ( “ %s “, s1. name ) ;
}                                                                            Prints P.Kiran
                                           printf ( “ %s “ , s1.next - > name ) ;   Prints B.Mahesh
OUTPUT:                                }
           7 / 12



         A self referential structure is one that includes at least one member
                     which is a pointer to the same structure type.
             With self referential structures, we can create very useful data
      structures such as linked -lists, trees and graphs.
Pointer to a structure

struct product                                     Accessing structure members through
{                                                  pointer :
   int prodid;
   char name[20];                                  i) Using . ( dot ) operator :
};                                                      ( *ptr ) . prodid = 111 ;
                                                        strcpy ( ( *ptr ) . Name, “Pen”) ;
int main()
{                                                  ii) Using - > ( arrow ) operator :
   struct product inventory[3];                          ptr - > prodid = 111 ;
   struct product *ptr;                                  strcpy( ptr - > name , “Pencil”) ;
   printf(“Read Product Details : n");
   for(ptr = inventory;ptr<inventory +3;ptr++) {
     scanf("%d %s", &ptr->prodid, ptr->name);      Read Product Details :
   }
                                                   111 Pen
   printf("noutputn");
                                                   112 Pencil
   for(ptr=inventory;ptr<inventory+3;ptr++)        113 Book
   {
     printf("nnProduct ID :%5d",ptr->prodid);    Print Product Details :
     printf("nName : %s",ptr->name);
   }                                               Product ID : 111
}                                                  Name : Pen
                                                   Product ID : 112
                                                   Name : Pencil
                                                   Product ID : 113
                                                   Name : Book
A union is a structure all of whose members share the same memory
        Union is a variable, which is similar to the structure and contains number of members
like structure.
       In the structure each member has its own memory location whereas, members of union
share the same memory. The amount of storage allocated to a union is sufficient to hold its
largest member.
struct student {
    int rollno;                              Memory allotted to structure student
    float avg ;
    char grade ;                     Address 5000     5001     5002    5003    5004 5005  5006
};
union pupil {
    int rollno;
    float avg ;                                  rollno                    avg           grade
    char grade;                                     Total memory occupied : 7 bytes
};
int main() {
    struct student s1 ;                           Memory allotted to union pupil
    union pupil p1;
    printf ( “ %d bytes “,            Address 5000        5001    5002    5003
       sizeof ( struct student ) ) ;
     printf ( “ %d bytes “,
        sizeof ( union pupil ) ) ;
}                                                  rollno
Output :                                                     avg
   7 bytes 4 bytes                             grade
                                              Total memory occupied : 4 bytes

Contenu connexe

Tendances

PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
Fitz Agard
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
MongoSF
 
Linear algebra review
Linear algebra reviewLinear algebra review
Linear algebra review
vevin1986
 

Tendances (12)

PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
 
Jazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with EclipseJazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with Eclipse
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Linear algebra review
Linear algebra reviewLinear algebra review
Linear algebra review
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Php Map Script Class Reference
Php Map Script Class ReferencePhp Map Script Class Reference
Php Map Script Class Reference
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 

Similaire à C Language Unit-4

Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
mrecedu
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
Alisha Korpal
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
babuk110
 

Similaire à C Language Unit-4 (20)

Unit4 C
Unit4 C Unit4 C
Unit4 C
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
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
 
structure.ppt
structure.pptstructure.ppt
structure.ppt
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structures
StructuresStructures
Structures
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdf
 
STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING STRUCTURES IN C PROGRAMMING
STRUCTURES IN C PROGRAMMING
 
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
 
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
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Presentation on structure,functions and classes
Presentation on structure,functions and classesPresentation on structure,functions and classes
Presentation on structure,functions and classes
 
structure
structurestructure
structure
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 

Plus de kasaragadda srinivasrao (7)

C Language Unit-8
C Language Unit-8C Language Unit-8
C Language Unit-8
 
C Language Unit-7
C Language Unit-7C Language Unit-7
C Language Unit-7
 
C Language Unit-6
C Language Unit-6C Language Unit-6
C Language Unit-6
 
C Language Unit-5
C Language Unit-5C Language Unit-5
C Language Unit-5
 
C-Language Unit-2
C-Language Unit-2C-Language Unit-2
C-Language Unit-2
 
C Language Unit-1
C Language Unit-1C Language Unit-1
C Language Unit-1
 
Coupon tango site demo
Coupon tango site demoCoupon tango site demo
Coupon tango site demo
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

C Language Unit-4

  • 1. C Data Types: Primary data types Derived Derived data types Types User-defined data types Function Pointer Structure Array Type Union Type Type Type Type Array – Collection of one or more related variables of similar data type grouped under a single name Structure – Collection of one or more related variables of different data types, grouped under a single name In a Library, each book is an object, and its characteristics like title, author, no of pages, price are grouped and represented by one record. The characteristics are different types and grouped under a aggregate variable of different types. A record is group of fields and each field represents one characteristic. In C, a record is implemented with a derived data type called structure. The characteristics of record are called the members of the structure.
  • 2. Book-1 Book-2 Book-3 BookID: 1211 BookID: 1212 BookID: 1213 Title : C Primer Plus Title : The ANSI C Programming Title : C By Example Author : Stephen Prata Author : Dennis Ritchie Author : Greg Perry Pages : 984 Pages : 214 Pages : 498 Price : Rs. 585.00 Price : Rs. 125.00 Price : Rs. 305.00 book book_id integer 2 bytes bookid title Array of 50 characters 50 bytes title author Array of 40 characters 40 bytes author pages integer 2 bytes pages price price float 4 bytes Memory occupied by a Structure variable STRUCTURE- BOOK struct < structure_tag_name > struct book { { Structure tag data type < member 1 > int book_id ; char title[50] ; data type < member 2 > …. …. …. …. char author[40] ; data type < member N > int pages ; }; float price ; };
  • 3. Declaring a Structure Type Declaring a Structure Variable struct student struct student s1,s2,s3; { (or) int roll_no; struct student char name[30]; { float percentage; int roll_no; }; char name[30]; float percentage; }s1,s2,s3; Initialization of structure Reading values to members at runtime: Initialization of structure variable while declaration : struct student s3; struct student s2 = { 1001, “ K.Avinash ”, printf(“nEnter the roll no”); 87.25 } ; scanf(“%d”,&s3.roll_no); Initialization of structure members individually : printf(“nEnter the name”); s1. roll_no = 1111; scanf(“%s”,s3.name); strcpy ( s1. name , “ B. Kishore “ ) ; printf(“nEnter the percentage”); s1.percentage = 78.5 ; scanf(“%f”,&s3.percentage); membership operator
  • 4. Implementing a Structure struct employee { int empid; char name[35]; int age; Declaration of Structure Type float salary; }; Declaration of Structure variables int main() { struct employee emp1,emp2 ; Declaration and initialization of Structure variable struct employee emp3 = { 1213 , ” S.Murali ” , 31 , 32000.00 } ; emp1.empid=1211; strcpy(emp1.name, “K.Ravi”); emp1.age = 27; Initialization of Structure members individually emp1.salary=30000.00; printf(“Enter the details of employee 2”); Reading values to members of Structure scanf(“%d %s %d %f “ , &emp2.empid, emp2.name, &emp2.age, &emp2.salary); if(emp1.age > emp2.age) printf( “ Employee1 is senior than Employee2n” ); else printf(“Employee1 is junior than Employee2n”); Accessing members of Structure printf(“Emp ID:%dn Name:%sn Age:%dn Salary:%f”, emp1.empid,emp1.name,emp1.age,emp1.salary); }
  • 5. Arrays And structures Nesting of structures struct student struct date { { int day ; int sub[3] ; int month ; Outer Structure int total ; int year ; }; }; struct person { char name[40]; int main( ) { int age ; struct student s[3]; struct date b_day ; int i,j; }; for(i=0;i<3;i++) { int main( ) { Inner Structure printf(“nnEnter student %d marks:”,i+1); struct person p1; for(j=0;j<3;j++) { strcpy ( p1.name , “S. Ramesh “ ) ; scanf(“%d”,&s[i].sub[j]); p1. age = 32 ; } p1.b_day.day = 25 ; Accessing Inner } p1.b_day. month = 8 ; Structure members for(i=0;i<3;i++) { p1.b_day. year = 1978 ; s[i].total =0; } for(j=0;j<3;j++) { s[i].total +=s[i].sub[j]; OUTPUT: } Enter student 1 marks: 60 60 60 printf(“nTotal marks of student %d is: %d”, Enter student 2 marks: 70 70 70 i+1,s[i].total ); Enter student 3 marks: 90 90 90 } } Total marks of student 1 is: 180 Total marks of student 2 is: 240 Total marks of student 3 is: 270
  • 6. structures and functions Self referential structures struct fraction { struct student_node { int numerator ; int roll_no ; int denominator ; char name [25] ; }; struct student_node *next ; }; void show ( struct fraction f ) int main( ) { { printf ( “ %d / %d “, f.numerator, struct student_node s1 ; struct student_node s2 = { 1111, “B.Mahesh”, NULL } ; f.denominator ) ; s1. roll_no = 1234 ; } strcpy ( s1.name , “P.Kiran “ ) ; int main ( ) { s1. next = & s2 ; s2 node is linked to s1 node struct fraction f1 = { 7, 12 } ; show ( f1 ) ; printf ( “ %s “, s1. name ) ; } Prints P.Kiran printf ( “ %s “ , s1.next - > name ) ; Prints B.Mahesh OUTPUT: } 7 / 12 A self referential structure is one that includes at least one member which is a pointer to the same structure type. With self referential structures, we can create very useful data structures such as linked -lists, trees and graphs.
  • 7. Pointer to a structure struct product Accessing structure members through { pointer : int prodid; char name[20]; i) Using . ( dot ) operator : }; ( *ptr ) . prodid = 111 ; strcpy ( ( *ptr ) . Name, “Pen”) ; int main() { ii) Using - > ( arrow ) operator : struct product inventory[3]; ptr - > prodid = 111 ; struct product *ptr; strcpy( ptr - > name , “Pencil”) ; printf(“Read Product Details : n"); for(ptr = inventory;ptr<inventory +3;ptr++) { scanf("%d %s", &ptr->prodid, ptr->name); Read Product Details : } 111 Pen printf("noutputn"); 112 Pencil for(ptr=inventory;ptr<inventory+3;ptr++) 113 Book { printf("nnProduct ID :%5d",ptr->prodid); Print Product Details : printf("nName : %s",ptr->name); } Product ID : 111 } Name : Pen Product ID : 112 Name : Pencil Product ID : 113 Name : Book
  • 8. A union is a structure all of whose members share the same memory Union is a variable, which is similar to the structure and contains number of members like structure. In the structure each member has its own memory location whereas, members of union share the same memory. The amount of storage allocated to a union is sufficient to hold its largest member. struct student { int rollno; Memory allotted to structure student float avg ; char grade ; Address 5000 5001 5002 5003 5004 5005 5006 }; union pupil { int rollno; float avg ; rollno avg grade char grade; Total memory occupied : 7 bytes }; int main() { struct student s1 ; Memory allotted to union pupil union pupil p1; printf ( “ %d bytes “, Address 5000 5001 5002 5003 sizeof ( struct student ) ) ; printf ( “ %d bytes “, sizeof ( union pupil ) ) ; } rollno Output : avg 7 bytes 4 bytes grade Total memory occupied : 4 bytes