intoduction to structure

50
Title ~CONTENTS~ Introduction To Structures ~What is Structure ~An Array of Structure ~Pointer and Nested Structure ~Union~ ~What is Union ~Structure and Union

Upload: utsav276

Post on 15-Jan-2017

269 views

Category:

Education


0 download

TRANSCRIPT

Title

~CONTENTS~Introduction To Structures

~What is Structure~An Array of Structure

~Pointer and Nested Structure~Union~

~What is Union~Structure and Union

TitleWhat are Variables??

Symbolic representation of a value

Title Variables

• Used to store values in program.• Example:

Roll No int rollNo;

Percentage float percentage;

Student

Title Data-Types of C

• C language supports 4 fundamental data types :

Type Purpose

int For storing integers

float For storing real numbers

double For storing double precision floating

char for storing characters

Ordinary variable can store only one piece of information.

Title

Now if we want to store roll-No of 50 students…

Problem-Solution

ArrayExample :-

int rollNo[50];

Title

But there is a problem with array itself.

Again Problem

Array can only store information of similar data-type…!!

TitleReal World Object

Roll No int rollNo;

float percentage;

Student Name

PercentageGrade

char name[20];

char grade;

TitleReal World Object

Roll No int rollNo;

float percentage;

Student Name

PercentageGrade

char name[20];

char grade;

TitleReal World Object

Roll No int rollNo[50];

float percentage[50];

Student Name

PercentageGrade

char name[50][20];

char grade[50];

TitleWay of Data Representation

NameStudent A

Student B

Student C

Student D

Roll No1

2

3

4

Percentage85.6

74.3

65.12

48.65

GradeA

B

C

D

Student A Student B Student C Student D

TitleWant to define a New Datatype

Roll No

Student Name

PercentageGrade

Student

Title

Structure

Titlewhat is Structure?

Structure is a user-defined data-type which allows to combine and store

different kinds of data.

TitleDeclaring a Structure..

struct <structutre_name>{

element 1;element 2;...element n;

} ;

TitleDeclaring a Structure..

struct student{

char name[20] ;int rollNo ;float

percentage ;char grade ;

} ;

Student

NameRoll NoPercentageGrade

TitleDeclaring a Structure variable..

struct{

char name[20] ;int rollNo ;float

percentage ;char grade ;

} ;

struct student s1;name

rollNo

percentage

grade

s1

student

Title

struct student s1;

s1strcpy ( s1.name, “ Jay” );s1.percentage =85.23;s1.grade = ‘A’;

rollNo

Accessing Structure Elements

. = 10;

struct student {

char name[20] ;int rollNo ;float percentage

;char grade ;

} ;

TitleAccessing Structure Elements

struct student s1;

s1strcpy ( s1.name, “ Jay” );s1.percentage =85.23;s1.grade = ‘A’;

.rollNo = 10;“Jay”

10

85.23

A

s1

TitleAccessing Structure Elements

struct student s1;

scanf(“%d”, &s1.rollNo);gets ( s1.name);scanf(“%f”, &s1.percentage);scanf(“%c”, &s1.grade);

“Jay”

10

85.23

A

s1

TitleAccessing Structure Elements

struct student s1;

puts(s1.name);printf(“%d”, s1.rollNo);printf(“%lf”, s1.percentage);printf(“%c”, s1.grade);

.“Jay”

10

85.23

A

s1

TitleExample : Book (Question)

NameAuthorPriceEdition

Example for help:struct student{

char name[20] ;int rollNo ;float percentage

;char grade ;

} ;

TitleExample : Book (Solution)

NameAuthorPriceEdition

struct book{

char name[30] ;char

author[20] ;float price ;int edition ;

} ;

struct book{

char name[30] ;char author[20] ;float price ;int edition ;

} ;void main(){struct book b1 = { “Learn C”, “Rohit”, 1000 , 1};printf(“\n Book Name : %s ”, b1.name);printf(“\nAuthor : %s ”, b1.author);printf(“\nPrice : %f ”, b1.price);printf(“\nEdition : %d ”, b1.edition);}

Output:Book Name : Learn CAuthor : RohitPrice : 1000Edition : 1

Title Example : Book

NameAuthorPriceEdition

struct book{

char name[30] ;

char author[20] ;

float price ;int edition ;

} ;

struct book{

char name[30] ;char author[20] ;float price ;int edition ;

} ;void main(){struct book b1 = { “Let Us C”, “Yashwant K.”, 700 , 8};printf(“\n Book Name : %s ”, b1.name);printf(“\nAuthor : %s ”, b1.author);printf(“\nPrice : %f ”, b1.price);printf(“\nEdition : %d ”, b1.edition);}

Output:Book Name : Let Us CAuthor : Yashwant K.Price : 700Edition : 8

Title

Arrayof

Structure

void main(){struct book b[3];int i;for(i=0; i<3; i++){

scanf(“%s %f”, b[i].name, &b[i].price);}for(i=0; i<3; i++){

printf(“\nName : %s & Price :%d ”, b[i].name, b[i].price);

}}

struct book{

char name[30] ;

float price ;} ;

Example :

Output:Physics 1000Maths 450Chemistry 240

Name : Physics & Price : 1000Name : Maths & Price : 450Name : Chemistry & Price : 240

Example :

void main(){int i;struct book b[3];for(i=0; i<3; i++){

scanf(“%s %f”, b[i].name, &b[i].price);}for(i=0; i<3; i++){

}}

Example : struct book{

char name[30] ;

float price ;} ;

printf(“\nName : %s & Price :%d ”, b[i].name, b[i].price);

void main(){int i;struct book b[3];for(i=0; i<3; i++){

scanf(“%s %f”, b[i].name, &b[i].price);}for(i=0; i<3; i++){

display(b[i]);}}

Example : struct book{

char name[30] ;

float price ;} ;

void display( struct book x ){printf(“\nName : %s & Price :%d ”, x.name, x.price);}

void main(){int i;struct book b[3];for(i=0; i<3; i++){

scanf(“%s %f”, b[i].name, &b[i].price);}

display(b);}

Example : struct book{

char name[30] ;

float price ;} ;

void display( struct book x[ ] ){int i;for(i=0; i<3; i++)

printf (“\nName : %s & Price :%d ”, x[i].name, x[i].price);}

Title

Pointer of Structure&

Nested Structure

Title Structure Pointer

float x = 14.5;float *px = &x;

struct Emp e;struct Emppe = &e;

struct Emp {int age;float salary;

} ;

*pe;

Member access through Pointer

struct Emp e = {25,35000};struct Emp *pe = &e;

printf(“Age : %d”, e.age);printf(“Age : %d”, *pe);

struct Emp {int age;float salary;

} ;

25

35000pe 2000

2000

e*pe e

Member access through Pointer

struct Emp e = {25,35000};struct Emp *pe = &e;

printf(“Age : %d”, e.age);printf(“Age : %d”, (*pe).age);printf(“Salary : %f”, (*pe).salary);

struct Emp {int age;float salary;

} ;

TitleMember Selection Operator

->Arrow Operator

Member Selection OperatorExample:

(*pe).age pe->age

TitleMember Selection Operator

struct Emp e = {25,35000};struct Emp *pe = &e;

printf(“Age : %d”, (*pe).age);printf(“Age : %d”, pe->age);

printf(“Salary : %f”, (*pe).salary);printf(“Salary : %f”, pe->salary);

struct Emp {int age;float salary;

} ;

Title Nested Structure

Nested Structure

(Structure within Structure)

struct student {char name[10];

} ;

struct result{char

subject[10];int mark;char grade;

} ;

char subject[10];

int mark;char grade;

Nested Structure

struct student {char name[10];

struct result rsl;

} ;

struct result{char

subject[10];int mark;char grade;

} ;Example:

struct student s; s.rsl.mark =10;

Nested Structure

struct student {char name[10];

struct result rsl;

} ;

struct {char

subject[10];int mark;char grade;

} ;

Nested Structure

struct student {char name[10];

} ;

struct {char

subject[10];int mark;char grade;

} rsl ;

Example:rsl.mark =10;

Nested Structure

struct student {char name[10]; struct {

char subject[10];

int mark;char grade;

} rsl ;} ;

Nested Structure

Nested Structure

struct student {char name[10];

struct {char

subject[10];int mark;char grade;

} rsl ;} ;

Example:struct student

s;

sname

rsl

Nested Structure

struct student {char name[10];

struct {char

subject[10];int mark;char grade;

} rsl ;} ;

Example:struct student

s; s.rsl.mark =10;

subject

mark

name

grade

srsl

Nested Structure

struct student {char name[10];

struct {char

subject[10];int mark;char grade;

} rsl ;} ;

Example:struct student

s; s.rsl.mark =10;

subject

10

name

grade

srsl

Example : Nested Structure

struct student {char name[10];

struct {char

subject[10];int mark;char grade;

} rsl ;}

void main(){struct student s;strcpy(s.name, “Joy”);strcpy(s.rsl.subject, “C”);s.rsl.mark = 95;s.rsl.grade = ‘a’;}

TitleStructure vs. Union

Structure Vs. Union

TitleStructure vs. UnionStructure Union

1.The keyword struct is used to define a structure 1. The keyword union is used to define a union.

2. When a variable is associated with a structure, the compiler allocates the memory for each member.

2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest member.

3. The size of structure is greater than or equal to the sum of sizes of its members.

3. The size of union is equal to the size of largest member.

4. Each member within a structure is assigned unique storage area of location.

4. Memory allocated is shared by individual members of union.

5. The address of each member will be in ascending order. This indicates that memory for each member will start at different offset values.

5. The address is same for all the members of a union. This indicates that every member begins at the same offset value.

6 Altering the value of a member will not affect other members of the structure.

6. Altering the value of any of the member will alter other member values.

7. Individual member can be accessed at a time 7. Only one member can be accessed at a time.

8. Several members of a structure can initialize at once.

8. Only the first member of a union can be initialized.

Title

Thank You .

Do visit us atwww.codeitoff.blogspot.com