Cpr Transperencies - Ch 04 Structures

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cpr Transperencies - Ch 04 Structures as PDF for free.

More details

  • Words: 578
  • Pages: 9
Chapter no. 4 Functions and Structures

Structure ‹ In general, we can call a structure is a collection of different types of data. ‹ A Structure contains number of data types grouped together. These data types may be or may not be of same data type. C’s Implementation of Structure • The keyword ‘struct’ is used for creating a structure. Syntax:

struct structure-name { datatype1 varname1; datatype1 varname2; datatype1 varname3; }; creating the variables of structure: struct structure-name var1, var2, var3;

-1-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Example:

struct list { int roll; char name[10]; float marks; };

Representation of structure: roll

Integer

name

Array of 10 characters

marks

Float

-2-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Arrays vs Structures • An array is the collection of related data elements of same type. Structure can have elements in different types. • An array is derived data type, whereas a structure is programmer defined data type. • An array behaves like a built-in data type. All we have to do is to declare an array variable and use it. But, in case of a structure, first we have to design and declare data structure before the variables of that type are declared and used.

-3-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Accessing Structure Members

struct list { int roll; char name[10]; float marks; }; struct list stu1, stu2; stu1.roll = 12; stu1.marks = 85.63; strcpy(stu1.name, “Ajay”); stu2.marks = 81.22; stu2.roll = 10; or scanf(“%s”, stu2.name); scanf(“%d”, &stu1.roll); scanf(“%f”, &stu2.marks);

-4-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Valid declaration. struct list { int roll; char name[10]; float marks; }; struct list a , b , c; It is equivalent to : struct list { int roll; char name[10]; float marks; }

a, b, c;

-5-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Structure Initialization

struct player { int matches, runs; char name[10]; float average; }; struct player p1, p2, p3; p1 = {120,3450,“Raj”,42.1}; p2 = {71,1492,“Ram”,35.81};

printf(“Average of %s is %f”, p1.name, p1.average);

-6-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Example: Creating a student database of 3 students. void main( )

{ struct student { int roll; char name[10]; float marks; } a, b, c; printf(“Enter all information of students:” ); scanf(“%d %s %f”, &a.roll, a.name, &a.marks); scanf(“%d %s %f”, &b.roll, b.name, &b.marks); scanf(“%d %s %f”, &c.roll, c.name, &c.marks); printf(“You entered this information:”); printf(“\n%d %s %f”, a.roll, a.name, a.marks); printf(“\n%d %s %f”, b.roll, b.name, b.marks); printf(“\n%d %s %f”, c.roll, c.name, c.marks);

}

-7-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Array of structures. void main( ) { struct student { int roll; char name[10]; float marks; } a[10]; int i; printf(“Enter info. of students: ”); for(i = 0 ; i < 10 ; i++) { scanf(“%d %s %f”, &a.roll[i], a.name[i], &a.marks[i]); } for(i = 0 ; i < 10 ; i++) { printf(“\n%d %s %f”, a.roll[i], a.name[i], a.marks[i]); } } -8-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Memory map for arrays.

a[i].roll

i

a[i].name a[i].marks

0 1 2

a[4].roll

3 4 5 6 7 8

a[6].name

9

a[9].marks

-9-

By, Kute T. B. for CPR (FYIF) 2007-08

Related Documents