SlideShare une entreprise Scribd logo
1  sur  19
Structures




January 5, 2013   Programming and   1
What is a Structure?

• It is a convenient tool for handling a group of
  logically related data items.
   – Student name, roll number, and marks
   – Real part and complex part of a complex number
• This is our first look at a non-trivial data
  structure.
   – Helps in organizing complex data in a more
     meaningful way.
• The individual structure elements are called
  members.

January 5, 2013     Programming and        2
Defining a Structure

• The composition of a structure may be defined
  as:
     struct tag {
                     member 1;
                     member 2;
                     :
                     member m;
                    };
   – struct is the required keyword.
   – tag is the name of the structure.
   – member 1, member 2, … are individual member
     declarations.

January 5, 2013     Programming and      3
Contd.

• The individual members can be ordinary
  variables, pointers, arrays, or other structures.
   – The member names within a particular structure
     must be distinct from one another.
   – A member name can be the same as the name of a
     variable defined outside of the structure.
• Once a structure has been defined, individual
  structure-type variables can be declared as:
    struct tag variable_1, variable_2, …, variable_n;



January 5, 2013   Programming and          4
Example

• A structure definition:
     struct student {
                                     char name[30];
                                     int roll_number;
                                     int total_marks;
                                     char dob[10];
                                };
• Defining structure variables:
        A new data-type




     struct student a1, a2, a3;
                          }
January 5, 2013               Programming and           5
A Compact Form

•   It is possible to combine the declaration of the
    structure with that of the structure variables:
    struct tag {
                     member 1;
                     member 2;
                     :
                     member m;
                   } variable_1, variable_2,…, variable_n;
•   In this form, “tag” is optional.



January 5, 2013    Programming and          6
Example
   struct student {
                         char name[30];
                         int roll_number;
                         int total_marks;
                         char dob[10];
                                                Equivalent
                      } a1, a2, a3;
                                                declarations
   struct             {
                        char name[30];
                        int roll_number;
                        int total_marks;
                        char dob[10];
                      } a1, a2, a3;


January 5, 2013   Programming and           7
Processing a Structure

• The members of a structure are processed
  individually, as separate entities.
• A structure member can be accessed by writing
     variable.member
  where variable refers to the name of a
  structure-type variable, and member refers to
  the name of a member within the structure.
• Examples:
   – a1.name, a2.name, a1.roll_number, a3.dob;


January 5, 2013   Programming and         8
Example: Complex number addition

#include <stdio.h>                            printf (“n %f + %f j”, c.real,
struct complex                                                  c.complex);
                       Scope
                                          }
    {                restricted
        float real;    within
        float complex; main()
                                              Structure definition
     };
                                                      And
main()                                        Variable Declaration
{
struct complex a,b,c;

  scanf (“%f %f”, &a.real, &a.complex);
  scanf (“%f %f”, &b.real, &b.complex);
                                                    Reading a member
                                                         variable
  c.real = a.real + b.real;
  c.complex = a.complex members
           Accessing + b.complex;
   January 5, 2013      Programming and                  9
Comparison of Structure Variables

• Unlike arrays, group operations can be
  performed with structure variables.
   – A structure variable can be directly assigned to
     another structure variable of the same type.
               a1 = a2;
       • All the individual members get assigned.
   – Two structure variables can be compared for
     equality or inequality.
              if (a1 = = a2) …….
       • Compare all members and return 1 if they are equal; 0
         otherwise.


January 5, 2013     Programming and                 10
Arrays of Structures

• Once a structure has been defined, we can
  declare an array of structures.
     struct student class[50];

   – The individual members can be accessed as:
       • class[i].name
       • class[5].roll_number




January 5, 2013     Programming and       11
Arrays within Structures

• A structure member can be an array:
                  struct student {
                                        char name[30];
                                        int roll_number;
                                        int marks[5];
                                        char dob[10];
                                     } a1, a2, a3;

• The array element within the structure can be
  accessed as:
     a1.marks[2]

January 5, 2013      Programming and             12
Defining data type: using typedef

• One may define a structure data-type with a
  single name.
• General syntax:
     typedef struct {
                      member-variable1;
                      member-variable2;
                         .
                      member-variableN;
                    } tag;
• tag is the name of the new data-type.

January 5, 2013   Programming and    13
typedef : An example


typedef struct{
          float real;
          float imag;
          } _COMPLEX;

_COMPLEX a,b,c;



January 5, 2013   Programming and    14
Structure Initialization


• Structure variables may be initialized following
  similar rules of an array. The values are
  provided within the second braces separated by
  commas.
• An example:
_COMPLEX a={1.0,2.0}, b={-3.0,4.0};

                   a.real=1.0; a.imag=2.0;
                   b.real=-3.0; b.imag=4.0;

January 5, 2013   Programming and        15
Parameter Passing in a Function

• Structure variables could be passed as
  parameters like any other variable. Only the
  values will be copied during function
  invokation.
       void swap(_COMPLEX a, _COMPLEX b)
       {
         _COMPLEX tmp;

           tmp=a;
           a=b;
           b=tmp;
       }
January 5, 2013     Programming and   16
An example program
#include <stdio.h>

typedef struct{
      float real;
      float imag;
     } _COMPLEX;
  void swap(_COMPLEX a, _COMPLEX b)
   {
     _COMPLEX tmp;

    tmp=a;
    a=b;
    b=tmp;
   }
January 5, 2013   Programming and   17
Example program: contd.
 void print(_COMPLEX a)
 {
  printf("(%f , %f) n",a.real,a.imag);
 }

 main()
 {
  _COMPLEX x={4.0,5.0},y={10.0,15.0};

     print(x); print(y);
     swap(x,y);
     print(x); print(y);
 }
January 5, 2013      Programming and      18
Returning structures

• It is also possible to return structure values
  from a function. The return data type of the
  function should be as same as the data type of
  the structure itself.
       _COMPLEX add(_COMPLEX a, _COMPLEX b)
       {
         _COMPLEX tmp;
                                       Direct arithmetic
         tmp.real=a.real+b.real;
                                      operations are not
         tmp.imag=a.imag+b.imag;
                                         possible with
                                      Structure variables.
         return(tmp);
       }
January 5, 2013     Programming and      19

Contenu connexe

Tendances

Tendances (20)

Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithms
 
Datastructures using c++
Datastructures using c++Datastructures using c++
Datastructures using c++
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Lecture 01 Intro to DSA
Lecture 01 Intro to DSALecture 01 Intro to DSA
Lecture 01 Intro to DSA
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Data structures
Data structuresData structures
Data structures
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Data Structures
Data StructuresData Structures
Data Structures
 
Data Structure
Data StructureData Structure
Data Structure
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structure
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 

En vedette

linked list
linked listlinked list
linked listAbbott
 
5 Array List, data structure course
5 Array List, data structure course5 Array List, data structure course
5 Array List, data structure courseMahmoud Alfarra
 
Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)Adam Mukharil Bachtiar
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introductionSugandh Wafai
 
data structure and algorithms
data structure and algorithmsdata structure and algorithms
data structure and algorithmsibrar ahmad
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 PolymorphismMahmoud Alfarra
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structureMahmoud Alfarra
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....Shail Nakum
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceTransweb Global Inc
 

En vedette (20)

01 05 - introduction xml
01  05 - introduction xml01  05 - introduction xml
01 05 - introduction xml
 
linked list
linked listlinked list
linked list
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
5 Array List, data structure course
5 Array List, data structure course5 Array List, data structure course
5 Array List, data structure course
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
 
Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)Data Structure (Introduction to Data Structure)
Data Structure (Introduction to Data Structure)
 
Data structures
Data structuresData structures
Data structures
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
 
data structure and algorithms
data structure and algorithmsdata structure and algorithms
data structure and algorithms
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Data Structures
Data StructuresData Structures
Data Structures
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
 
7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 

Similaire à L6 structure

Similaire à L6 structure (20)

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++
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structure & union
Structure & unionStructure & union
Structure & union
 
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++
 
1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structures
StructuresStructures
Structures
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.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
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
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
StructuresStructures
Structures
 

Plus de mondalakash2012 (13)

Snns
SnnsSnns
Snns
 
Sn reaction
Sn reactionSn reaction
Sn reaction
 
Part i
Part iPart i
Part i
 
Extra problem for 1st yr
Extra problem for 1st yrExtra problem for 1st yr
Extra problem for 1st yr
 
Elimination
EliminationElimination
Elimination
 
L12 complexity
L12 complexityL12 complexity
L12 complexity
 
L11 tree
L11 treeL11 tree
L11 tree
 
L10 sorting-searching
L10 sorting-searchingL10 sorting-searching
L10 sorting-searching
 
L8 file
L8 fileL8 file
L8 file
 
L4 functions
L4 functionsL4 functions
L4 functions
 
L3 control
L3 controlL3 control
L3 control
 
L2 number
L2 numberL2 number
L2 number
 
Struct examples
Struct examplesStruct examples
Struct examples
 

L6 structure

  • 1. Structures January 5, 2013 Programming and 1
  • 2. What is a Structure? • It is a convenient tool for handling a group of logically related data items. – Student name, roll number, and marks – Real part and complex part of a complex number • This is our first look at a non-trivial data structure. – Helps in organizing complex data in a more meaningful way. • The individual structure elements are called members. January 5, 2013 Programming and 2
  • 3. Defining a Structure • The composition of a structure may be defined as: struct tag { member 1; member 2; : member m; }; – struct is the required keyword. – tag is the name of the structure. – member 1, member 2, … are individual member declarations. January 5, 2013 Programming and 3
  • 4. Contd. • The individual members can be ordinary variables, pointers, arrays, or other structures. – The member names within a particular structure must be distinct from one another. – A member name can be the same as the name of a variable defined outside of the structure. • Once a structure has been defined, individual structure-type variables can be declared as: struct tag variable_1, variable_2, …, variable_n; January 5, 2013 Programming and 4
  • 5. Example • A structure definition: struct student { char name[30]; int roll_number; int total_marks; char dob[10]; }; • Defining structure variables: A new data-type struct student a1, a2, a3; } January 5, 2013 Programming and 5
  • 6. A Compact Form • It is possible to combine the declaration of the structure with that of the structure variables: struct tag { member 1; member 2; : member m; } variable_1, variable_2,…, variable_n; • In this form, “tag” is optional. January 5, 2013 Programming and 6
  • 7. Example struct student { char name[30]; int roll_number; int total_marks; char dob[10]; Equivalent } a1, a2, a3; declarations struct { char name[30]; int roll_number; int total_marks; char dob[10]; } a1, a2, a3; January 5, 2013 Programming and 7
  • 8. Processing a Structure • The members of a structure are processed individually, as separate entities. • A structure member can be accessed by writing variable.member where variable refers to the name of a structure-type variable, and member refers to the name of a member within the structure. • Examples: – a1.name, a2.name, a1.roll_number, a3.dob; January 5, 2013 Programming and 8
  • 9. Example: Complex number addition #include <stdio.h> printf (“n %f + %f j”, c.real, struct complex c.complex); Scope } { restricted float real; within float complex; main() Structure definition }; And main() Variable Declaration { struct complex a,b,c; scanf (“%f %f”, &a.real, &a.complex); scanf (“%f %f”, &b.real, &b.complex); Reading a member variable c.real = a.real + b.real; c.complex = a.complex members Accessing + b.complex; January 5, 2013 Programming and 9
  • 10. Comparison of Structure Variables • Unlike arrays, group operations can be performed with structure variables. – A structure variable can be directly assigned to another structure variable of the same type. a1 = a2; • All the individual members get assigned. – Two structure variables can be compared for equality or inequality. if (a1 = = a2) ……. • Compare all members and return 1 if they are equal; 0 otherwise. January 5, 2013 Programming and 10
  • 11. Arrays of Structures • Once a structure has been defined, we can declare an array of structures. struct student class[50]; – The individual members can be accessed as: • class[i].name • class[5].roll_number January 5, 2013 Programming and 11
  • 12. Arrays within Structures • A structure member can be an array: struct student { char name[30]; int roll_number; int marks[5]; char dob[10]; } a1, a2, a3; • The array element within the structure can be accessed as: a1.marks[2] January 5, 2013 Programming and 12
  • 13. Defining data type: using typedef • One may define a structure data-type with a single name. • General syntax: typedef struct { member-variable1; member-variable2; . member-variableN; } tag; • tag is the name of the new data-type. January 5, 2013 Programming and 13
  • 14. typedef : An example typedef struct{ float real; float imag; } _COMPLEX; _COMPLEX a,b,c; January 5, 2013 Programming and 14
  • 15. Structure Initialization • Structure variables may be initialized following similar rules of an array. The values are provided within the second braces separated by commas. • An example: _COMPLEX a={1.0,2.0}, b={-3.0,4.0}; a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0; January 5, 2013 Programming and 15
  • 16. Parameter Passing in a Function • Structure variables could be passed as parameters like any other variable. Only the values will be copied during function invokation. void swap(_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; tmp=a; a=b; b=tmp; } January 5, 2013 Programming and 16
  • 17. An example program #include <stdio.h> typedef struct{ float real; float imag; } _COMPLEX; void swap(_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; tmp=a; a=b; b=tmp; } January 5, 2013 Programming and 17
  • 18. Example program: contd. void print(_COMPLEX a) { printf("(%f , %f) n",a.real,a.imag); } main() { _COMPLEX x={4.0,5.0},y={10.0,15.0}; print(x); print(y); swap(x,y); print(x); print(y); } January 5, 2013 Programming and 18
  • 19. Returning structures • It is also possible to return structure values from a function. The return data type of the function should be as same as the data type of the structure itself. _COMPLEX add(_COMPLEX a, _COMPLEX b) { _COMPLEX tmp; Direct arithmetic tmp.real=a.real+b.real; operations are not tmp.imag=a.imag+b.imag; possible with Structure variables. return(tmp); } January 5, 2013 Programming and 19