structured programming approach module viii - additional c data types structures prof: muhammed...

31
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Upload: allyson-skinner

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Structured Programming Approach

Module VIII - Additional C Data TypesStructures

Prof: Muhammed Salman Shamsi

Page 2: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

STRUCTURE

• A structure is a collection of variables under a single name.

• These variables can be of different types, and each has a name that is used to select it from the structure.

• There can be structures within structures, which is known as nesting of structures.

• Arrays of structures can be formed and initialized as required. Pointers may also be used with structures.

• Structures may be passed as function arguments and they may also be returned by functions.

Page 3: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Declaring Structures and Structure Variables

• A structure is declared by using the keyword struct followed by an optional structure tag followed by the body of the structure. – The variables or members of the structure are declared within the

body.– The general format of declaring a simple structure is given as

follows.

Page 4: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Cont.

• The structure_tag_name is the name of the structure. The structure_variables are the list of variable names separated by commas.

• There are three different ways to declare and/or define a structure. These are– Variable structure– Tagged structure– Type-defined structure

• A variable structure may be defined as follows:struct

{

member_list

}variable_identifier;

Page 5: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Cont.

• 1.A structure can be defined as a user-defined data type that is capable of holding heterogeneous data of basic data type.

• 2. The structure is simply a type template with no associate storage.

• 3. The proper place for structure declarations is in the global area of the program before main().

• 4. It is not possible to compare structures for equality using ‘==’, nor is it possible to perform arithmetic on structures.

Page 6: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Accessing the Members of a Structure

• The members of a structure can be accessed in three ways. – One of the ways consists of using the ‘.’, which is known as the

‘dot operator’. – The members are accessed by relating them to the structure

variable with a dot operator.

• The general form of the statement for accessing a member of a structure is as follows:< structure_variable >.< member_name > ;

Page 7: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Initialization of Structures

• Structures that are not explicitly initialized by the programmer are, by default, initialized by the system.– In most of the C compilers, for integer and float data type

members, the default value is zero .

– For char and string type members the default value is ‘\0’.

– Example:

Page 8: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Cont.

• The general construct for initializing a structure can be any of the two forms given as follows.struct <structure_tag_name>

{

<data_type member_name1>;

<data_type member_name2>;

}<structure_variable1> = {constant1,constant2, . .};

or

struct <structure_tag_name> <structure_variable> ={constant1,constant2,..};

Page 9: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Copying & Comparing Structures

• A structure can be assigned to another structure of the same type. – Here is an example of assigning one structure to another.

– Comparing one structure variable with another is not allowed in C.

– However, when comparing two structures, one should compare the individual fields in the structure. Example:

Page 10: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Typedef and its Use in Structure Declarations

• The typedef keyword allows the programmer to create a new data type name for an existing data type.– The general form of the declaration statement using the typedef

keyword is given as follows. typedef <existing data type> <new data type ,….>;– The typedef statement does not occupy storage; it simply

defines a new type.– typedef statements can be placed anywhere in a C program as

long as they come prior to their first use in the code.• The following examples show the use of typedef.

– typedef int id_number;– typedef float weight;– typedef char lower_case;

Page 11: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Nesting of Structures

• A structure can be placed within another structure. – In other words, structures can contain other structures as

members.– A structure within a structure means nesting of structures.

• In such cases, the dot operator in conjunction with the structure variables are used to access the members of the innermost as well as the outermost structures.– It must be noted that an innermost member in a nested structure

can be accessed by chaining all the concerned structure variables, from outermost to innermost, with the member using the dot operator.

Page 12: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Arrays of Structures

• The structure variable would be an array of objects, each of which contains the member elements declared within the structure construct.

• The general construct for declaration of an array structure is given as follows:struct <structure_tag_name >

{

<data_type member_name1>;

<data_type member_name2>;

.. .

}<structure_variable>[index];

Or

struct <structure_tag_name> <structure_variable>[index];

Page 13: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Example: Arrays of Structures

Page 14: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Initializing Arrays of Structures

• Initializing arrays of structures is carried out in much the same way as arrays of standard data types.– A typical construct for initialization of an array of structures would

appear as follows:

Page 15: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Arrays within the Structure

• An innermost member in a nested structure can be accessed by chaining all the concerned structure variables, from outermost to innermost, with the member using the dot operator.– Example:

Page 16: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Structures and Pointers• At times it is useful to assign pointers to structures.

– A pointer to a structure is not itself a structure, but merely a variable that holds the address of a structure.

– This pointer variable takes four bytes of memory just like any other pointer in a 32-bit machine.

– Declaring pointers to structures is basically the same as declaring a normal pointer.

– There are many reasons for using a pointer to a struct. One of them is to make a two-way communication possible within functions.

– This aspect is explained with examples in the following section.– A pointer to a structure is not itself a structure, but merely a variable

that holds the address of a structure.

Page 17: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Structures and Pointers

A typical construct for declaring a pointer to a structure will appear as follows:

struct <structure_tag_name >

/* structure declaration */

{

<data_type member_name_1>;

<data_type member_name_2>;

. . .

<data_type member_name_n>;

}*ptr;

struct <structure_tag_name>{<data_type member_name_1>;<data_type member_name_2>;…<data_type member_name_n>;};struct <structure_tag_name>

*ptr;

Page 18: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Structures and Functions

• Passing and working with pointers to large structures may be more efficient while passing structures to a function and working within it. – When a structure is passed as an argument, each member of

the structure is copied. – In fact, each member is passed by value. In case the member is

an array, a copy of this array is also passed. – This can prove to be inefficient where structures are large or

functions are called frequently.– The general construct for passing a structure to a function and

returning a structure is

struct structure_tag function_name(struct

structure_tag structure_variable);

Page 19: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Initializing Structures Ex 1#include<stdio.h>struct employee{char name[35],dept[25];unsigned int salary;}e1={"ALLEN","COMPUTER",2500};

int main(){

struct employee e2={"SMITH","MECHANICAL",3000};printf("NAME\tDEPT\t\tSALARY\n");printf("%s\t%s\t%d\n",e1.name,e1.dept,e1.salary);printf("%s\t%s\t%d\n",e2.name,e2.dept,e2.salary);return 0;

}

Page 20: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Taking Structures Value form user

#include<stdio.h>struct employee{char name[35],dept[25];unsigned int salary;}e1,e2;

int main(){

printf("Enter the name,dept and salary of employee 1:\n");scanf("%s%s%d",e1.name,e1.dept,&e1.salary);printf("Enter the name,dept and salary of employee 2:\n");scanf("%s%s%d",e2.name,e2.dept,&e2.salary);printf("NAME\tDEPT\t\tSALARY\n");printf("%s\t%s\t%d\n",e1.name,e1.dept,e1.salary);printf("%s\t%s\t%d\n",e2.name,e2.dept,e2.salary);return 0;

}

Page 21: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Arrays of Structures

#include<stdio.h>struct employee{char name[35],dept[25];unsigned int salary;};

int main(){

int i;struct employee e[3]={

{"ALLEN","COMPUTER",2400},{"SMITH","MECHANICAL",2300},{"GATES","ELECTRICAL",2200}

};

Page 22: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

printf("NAME\tDEPT\t\tSALARY\n");for(i=0;i<3;i++)printf("%s\t%s\t%d\n",e[i].name,e[i].dept,e[i].salary);

return 0;}

Page 23: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Hospital Mgmt

#include<stdio.h>#include<string.h>struct patients{

char fname[20],mname[20],surname[20],disease[20];char dob[11];

};

int main(){

int i;char disease[20];

Page 24: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

struct patients p[4]={{"ALLEN","SMITH","DSOUZA","LUKEMIA","12-10-1980"},{"ALICE","SMITH","DSOUZA","TUBERCLOSIS","10-11-

1981"},{"GATES","WILL","RICE","LUKEMIA","22-11-1970"},{"BILL","ANTHONY","DSOUZA","CHOLERA","03-10-

1976"}};

printf("Enter the disease\n");scanf("%s",disease);printf("FNAME\tMNAME\tSURNAME\tDISEASE\tDOB\n");

}

Page 25: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

for(i=0;i<4;i++)if(strcmp(p[i].disease,disease)==0)

printf("\n%s\t%s\t%s\t%s\t%s",p[i].fname,p[i].mname,p[i].surname,p[i].disease,p[i].dob);

return 0;}

Page 26: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Club Data#include<stdio.h>struct player{

char name[30];int age,matches,runs;float average;

};int main(){

int i,n;struct player p[100];printf("\nEnter the number of players: ");scanf("%d",&n);printf("\nEnter the data for players");for(i=0;i<n;i++){

Page 27: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

printf("\nPLAYER %d",i+1);printf("\nName: ");scanf("%s",p[i].name);printf("\nAge: ");scanf("%d",&p[i].age);printf("\nNo. of Matches: ");scanf("%d",&p[i].matches);printf("\nNo. of Runs: ");scanf("%d",&p[i].runs);

p[i].average=(float)p[i].runs/p[i].matches;}

Page 28: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

printf("\nSports Club Data");printf("\nName\t\tAge\tMatches\tRuns\tAverage");for(i=0;i<n;i++)

printf("\n%s\t\t%d\t%d\t%d\t%f",p[i].name,p[i].age,p[i].matches,p[i].runs,p[i].average);return 0;

}

Page 29: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

Employees Data Sorted According to ID

#include<stdio.h>struct employee{

char name[35];unsigned int id,salary;

}e[100];int main(){

int i,j,n;printf("\nEnter the number of employees: ");scanf("%d",&n);printf("\nEnter the data for employees");

Page 30: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

for(i=0;i<n;i++){

printf("\nEmployee %d",i+1);printf("\nName: ");scanf("%s",e[i].name);printf("\nID: ");scanf("%d",&e[i].id);printf("\nSalary: ");scanf("%d",&e[i].salary);

}

Page 31: Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi

for(i=0;i<n-1;i++){

for(j=i+1;j<n;j++){

if(e[i].id>e[j].id){

struct employee temp;temp=e[i];e[i]=e[j];e[j]=temp;

}}

}printf("\nEmployees Data Sorted According to ID");printf("\nName\t\tID\tSalary");

for(i=0;i<n;i++)printf("\n%s\t\t%d\t%d",e[i].name,e[i].id,e[i].salary);

return 0;}