29

Storage Classes in c

  • Upload
    vijay

  • View
    217

  • Download
    1

Embed Size (px)

DESCRIPTION

Storage classes in c

Citation preview

Page 1: Storage Classes in c
Page 2: Storage Classes in c

• An ordinary variables can handle one piece of information however arrays can handle number of pieces of information but of same type.

• Structures in C are used to group together different data into one object i.e they contains number of data types that may or may not be of same type

Page 3: Storage Classes in c

Declaring a structure

• Syntax :- struct <structure name>{Structure element 1;Structure element 2;Structure element 3;-----};

Page 4: Storage Classes in c

• Eg:- struct mystruct{int num;char ch;}

• Structure has name mystruct and it contains two variables: an integer named num and a character named ch.

• The variables you declare inside the structure are called data members or member variables.

Page 5: Storage Classes in c

• This statement defines a new data type called struct mystruct. Each variable of this type will consist of character variable ch and integer variable num.

• Once new structure data type has been defined one or more variables can be declared to be of that type . Eg variable s1,s2 can be declared of type struct mystruct

struct mystruct s1,s2;

Page 6: Storage Classes in c

• struct mystruct s1,s2; :- This statement makes memory avaliable to hold all elements in structure.

• s1,s2 are called structure elements.

Page 7: Storage Classes in c

• Eg:- struct mystruct

{int num;char ch;};

struct mystruct s1,s2;

Page 8: Storage Classes in c

Syntax for using data members and structure type

struct mystruct{int num;char ch;}; struct mystruct s1,s2;

struct mystruct{int num;char ch;} s1,s2;

struct {int num;char ch;} s1,s2;

Page 9: Storage Classes in c

Accessing Structure Elements

• Once we declare structure type and structure variables, we begin with accessing elements of structure

• Elements in structures are accessed using dot operator(.)

• Syntax :- Structureelement.structure variable Eg:- s1.num

s2.ch

Page 10: Storage Classes in c

Accessing elements in Array vs structures

• In this elements are accessed using subscripts

• int a[5];

• They uses dot operator(.) for accessing elements

• s1.numb=12;s1.ch=’b’;printf(“\ns1.numb=%d”,s1.num);printf(“\ns1.ch=%c”,s1.ch);

Page 11: Storage Classes in c

How structure elements are stored

Main(){struct mystruct

{int num;char ch;};

struct mystruct s1={30},s2={‘B’};Printf(“\n integer value is =%d”,s1.num);Printf(“\n character value is =%c”,s2.char);}

Page 12: Storage Classes in c

In order to check address

Main(){struct mystruct

{int num;char ch;};

struct mystruct s1={30},s2={‘B’};Printf(“\n integer value is =%d”,&s1.num);Printf(“\n character value is =%c”,&s2.char);}

Page 13: Storage Classes in c

Array of structures

• Consider you want to store data of 100 books .In this case would required to use 100 different structure variables from b1 to b100 which is inconvenient.Therefore in this case we use array of structures.

Page 14: Storage Classes in c

Program using array of structureMain(){Struct book{Char name;Float price;Int pages;};Struct book b[100];Int I;For(i=0;<i<=99;i++){Printf(“\n enter name,prices and pages”);Scanf(“%c %f %d”,&b[i].name,&b[i].price,&b[i].pages);}For(i=0;i<=99;I++)printf(“%c %f %d”,b[i].name,b[i].price,b[i].pages);}

Page 15: Storage Classes in c

Structures and pointers

• The address of structures is accessed in same way i.e by using & operator

• Pointers can be used to refer to a struct by its address

Page 16: Storage Classes in c

• Syntax for declaring pointer variable for structure is Type*ptv

Here type is data type Ptv is name of pointer variable for structure.

• We assign address of structure variable to this pointer by writing

Ptv=&var

Page 17: Storage Classes in c

Variable and pointer declaration

• Struct{Member 1;Member 2;---}Var,*ptv;

Here var is structure type variable and ptv is pointer variable

Page 18: Storage Classes in c

• Individual structure member can be access in terms of pointer variable as

• Ptv->member • It is equivalent to (*ptv).member

Here -> operator is same as dot operator(.)

Page 19: Storage Classes in c

struct point { int x; int y; } my_point; struct point *p = &my_point; /* To declare p as a

pointer of type struct point */ (*p).x = 8; /* To access the first member of the struct */ p->x = 8; /* Another way to access the first member of

the struct */

Page 20: Storage Classes in c

Passing structures to functions

• Structure member can be transferred individually to structure or entire structure can be transferred.

• Individual structure member can be passes to function as aruguments in function call .

• Complete structure can be transferred to function by passing type pointer as arugument. In this struture is passed as refrence not as value.

Page 21: Storage Classes in c

• When structure is passed directly to function then it is passed by value.

Page 22: Storage Classes in c

•There are three methods by which the values of structure can be transferred from one function to another:

1. The first method is to pass each member of the structureas an actual argument of the function call.

The actual argument is then treated independently like ordinary variables.

Page 23: Storage Classes in c

•2. The second methods involve passing of a copy of the entire structure to the called function

Since the function is working on a copy of the entire structure to the called function, changes are not reflected in the original structure (in the calling function).

It is necessary for the entire function to return the entire structure back to the calling function.

Page 24: Storage Classes in c

•3. The third approach employs a concept called pointers to pass the structure as an argument .

In this case, the address location of the structure is passed to the called function.

The function can access indirectly the entire structure and work on it.

Page 25: Storage Classes in c

• The general format of sending a copy of structure to thecalled function is:

function_name (structure_variable_name)

Page 26: Storage Classes in c

struct p { int i; char c; }; void func(struct p*); void main() { struct p myp; myp.i=10; myp.c='a'; func(&myp); } void func(struct p *mp) { printf("%d %c",mp->i,mp->c); }

Page 27: Storage Classes in c

Features of structures

1. Values of structure variables can be assigned to another structure variables of same type using assignment operator

2. One structure can be nested with another structure

Page 28: Storage Classes in c

Nesting of structuresMain(){Struct address{Char phone[15];Char city[25];Int pin;};Struct emp{Char name[25];Struct address a;};Struct emp e={“ram”,”23456”,”chd”,10};Printf(“\nname=%s phone=%d”,e.name,e.a.phone);Printf(“\n city=%s pin=%d,e.a.city,e.a.pin);}

Page 29: Storage Classes in c

3. Like ordinary variables, structure variables can be passed to function