Storage Classes in c

Preview:

DESCRIPTION

Storage classes in c

Citation preview

• 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

Declaring a structure

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

• 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.

• 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;

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

• s1,s2 are called structure elements.

• Eg:- struct mystruct

{int num;char ch;};

struct mystruct s1,s2;

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;

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

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);

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);}

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);}

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.

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);}

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

• 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

Variable and pointer declaration

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

Here var is structure type variable and ptv is pointer variable

• 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(.)

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 */

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.

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

•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.

•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.

•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.

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

function_name (structure_variable_name)

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); }

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

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);}

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

Recommended