STRUCTURES Structures in C A structure is a

  • Slides: 21
Download presentation
STRUCTURES

STRUCTURES

Structures in C • A structure is – a convenient way of grouping several

Structures in C • A structure is – a convenient way of grouping several pieces of related information together – a collection of variables under a single name Examples : real number && imaginary number height && width && length complex number ( 3+5 i ) rectangular prism

Structures in C • The variables in structures can be of different types, and

Structures in C • The variables in structures can be of different types, and each has a name which is used to select it from the structure Example : ID (integer) && Name (char array) A Student record • A structure is a new named type that may involve several different typed variables

Defining Structures 1 “complex_number” is the tag name “struct complex_number” is the new type

Defining Structures 1 “complex_number” is the tag name “struct complex_number” is the new type struct complex_number { int real_part; int imaginary_part; };

Defining Structures 2 /* DEFINITION OF RECTANGULAR PRISM */ struct rectangular_prism { int height;

Defining Structures 2 /* DEFINITION OF RECTANGULAR PRISM */ struct rectangular_prism { int height; int width; int length; }; // name of new type? ? /* DEFINITION OF STUDENT RECORD */ struct student_record { int ID; char name[100]; };

Structures : Creating objects struct complex_number { int real_part; int imaginary_part; }; // Below

Structures : Creating objects struct complex_number { int real_part; int imaginary_part; }; // Below the definition of structure you can create // objects from it “s 1” and “s 2” struct complex_number s 1; struct complex_number s 2; s 1 real_part imaginary_part s 2 real_part imaginary_part

Structures : Creating objects struct rectangular_prism { int height; int width; int length; };

Structures : Creating objects struct rectangular_prism { int height; int width; int length; }; // Below the definition of structure you can create // objects from it “obj” struct rectangular_prism obj; obj height width length

Structures : Creating static arrays of objects struct student_record { int ID; char name[100];

Structures : Creating static arrays of objects struct student_record { int ID; char name[100]; }; // Creates an array of student records “group” group struct student_record group[4]; group[0] ID name group[1] ID name group[2] ID name group[3] ID name

Structures : Creating dynamic arrays of objects struct rec { int ID; char name[100];

Structures : Creating dynamic arrays of objects struct rec { int ID; char name[100]; }; // Creating a dynamic array // of student records “group” struct rec * group; // DECLARES POINTER group = ( struct rec * ) malloc ( sizeof (struct rec) * 4 ); size of objects in array number of objects in array (array size)

Structures : Accessing members of structures struct rectangular_prism { int height; int width; int

Structures : Accessing members of structures struct rectangular_prism { int height; int width; int length; }; // Create an object from the structure defined above “obj” struct rectangular_prism obj; // Members of the objects can be accessed by putting a dot // following the object name obj. height=10; obj. width=15; obj. length=40; height=10 width=15 length=40

Structures : Accessing members of structures struct rectangular_prism { int height; int width; int

Structures : Accessing members of structures struct rectangular_prism { int height; int width; int length; }; struct rectangular_prism obj; // Create a pointer to point object “obj” struct rectangular_prism *p = &obj; (*p). height=10 // or obj. height or p->height=10 p->width=15; p->length=40; obj == *p height=10 width=15 length=40

Structures : Accessing members of structures // Defines structure struct student_record { int ID;

Structures : Accessing members of structures // Defines structure struct student_record { int ID; char name[100]; }; // Creates an array of student records “group” struct student_record group[2]; group[0]. ID=200710; strcpy(group[0]. name, “doddy”); group[1]. ID=200711; strcpy(group[1]. name, group[0]. name); group[0] ID=200710 name= “doddy” group[1] name =? ? ID=200711

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char *

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s 1; struct record s 2; struct record s 3; Declaration 2 : struct record { int ID; char * name; char grade; } s 1, s 2; struct record s 3;

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char *

Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s 1; struct record s 2; Declaration 3 : struct { int ID; char * name; char grade; } s 1, s 2; /* no tag name */ /* no permission to declare other variables of this type */

Structures : DECLARATION ALTERNATIVES Declaration 4 : struct record { int ID; char *

Structures : DECLARATION ALTERNATIVES Declaration 4 : struct record { int ID; char * name; char grade; }; typedef struct record rec; rec s 1; struct record s 2; Declaration 5 : /* high degree of modularity and portability */ typedef struct { int ID; char * name; char grade; } rec; rec s 1; rec s 2;

Initialization of Structure Objects 1. struct names { char name[10]; int length; int weigth;

Initialization of Structure Objects 1. struct names { char name[10]; int length; int weigth; } man[3]= { “Tom”, 180, 65, “George”, 170, 68, “Bob”, 190, 100 }; 2. struct names woman[2]={{“Mary”, 170, 55}, {“Sue”, 160, 67}}; 3. struct names your; your. name=“Jane”; your. length=160; your. weigth=50;

Structures in Structures #include<stdio. h> struct physical_info { int length; int weigth; } ;

Structures in Structures #include<stdio. h> struct physical_info { int length; int weigth; } ; struct record { int salary; int working_hour; struct physical_info man; } ; main() { struct record s 1; s 1. salary=10000; s 1. working_hour= 6; s 1. man. length=180; s 1. man. weigth=78; }

Exercise 1 on Structures • Declare a structure for complex numbers (real and imaginary

Exercise 1 on Structures • Declare a structure for complex numbers (real and imaginary part) • Create 1 dynamic object (use pointers) • Ask user to fill the object • Print out the complex number as given below output Please give the values for complex number : 5 6 Complex number : 5 + 6 i

Exercise 1 on Structures struct complex { int real; int imaginary; }; main() {

Exercise 1 on Structures struct complex { int real; int imaginary; }; main() { struct complex * p; // Declare pointer for object // Memory allocation for object p=(struct complex *) malloc(sizeof(struct complex)); printf( " Please give the values for complex number : " ); scanf( "%d%d", &(p->real), & (p->imaginary) ); } printf( "Complex number : %d + %d i", p->real, p->imaginary );

Exercise 2 : Define a function to add two complex numbers // A function

Exercise 2 : Define a function to add two complex numbers // A function to add two integers int add(int a, int b) { int result= a+b; return result; } struct complex { int r; int i; }; // A function to add two complex numbers struct complex add ( struct complex a, struct complex b ) { struct complex result; result. r=a. r+b. r; result. i=a. i+b. i; return result; } Senem Kumova Metin

Exercise 3 on Structures struct rectangular_prism { int height; int width; int length; };

Exercise 3 on Structures struct rectangular_prism { int height; int width; int length; }; int volume(struct rectangular_prism x) {return x. height*x. width*x. length; } int area (struct rectangular_prism x) { return 2*x. width*x. lenght + 2*x. lenght*x. height + 2*x. width*x. lenght; }