Saturday 31 August 2013

Structures & pointers | C Programming Language



Structures & pointers:-

So far we have seen that pointer is a variable that hold the memory address of other var. of basic data type such as integer, float, char etc. A pointer can also be used in a program to hold the address of heterogeneous type of var .i.e. structure var. pointer along with structure are used to make linked lists, binary trees etc.
Ex:-
strtuct data
{
char n[10]
---
---
};
struct data * ptr;
*ptr is a ptr var.
Which holds the address of the structure named data?
The ptr is declared same as any object of a struct. Now, this ptr struct var can be accessed & processed by one of the following given two methods.

1) Any field of the structure can be accessed as:
(*ptr name ). Field name =var;
Ex:-
void main ()
{
struct data
{ int a ; float b; char c; };
struct data * ptr;
(*ptr) . a =100;
(*ptr) . b =20.9;
(*ptr). C =?m?;
}
The pointer to struct is expressed using a dashed line followed by (>) sign.
ptr name – >field name = var;

=> wap to use ptr s in structures using 2 methods
void main ()
{
struct data
{ int a; float b; char c ; };
struct data * ptr;
ptr a = 100;
ptr b = 20.9;
ptr c = „m? ;
-----
-----
};

Uses of structures:-
Structures are useful in database management i.e. to main to data about employees, but use of structures much beyond data base management. They can be used for.
1. Changing the size of cursor.
2. Clearing the eonents of the screen.
3. Drawing any graphical shape on the screen.
4. Placing the cursor at appropriate position on the screen.
5. Receiving a key from the keyboard.
6. Checking the memory size of the computer finding out the list of equipment attached to the computer.
7. Formatting a floppy.
8. Hiding a file from directly.
9. Display the directory of a disk.
10. Sending the o/p to printer.
11. Interacting with the mouse.

Terms within structures:-
ptr : can be used as a member of the structure.
Ex :
void main ()
{
struct student
{
int rno, *ptr;
};
struct student s;
s.ptr = * s.rno;
*s.ptr = 10;
printf (”rno = % d/n”, s.rno);
}

Structures & functionctions:-
Structures may be passed as an argument to a functionction in diff ways. We can pass individual members, who structures or struct ptr. To the function, similarly, a function. Can return either a struct. Member or a whole structure van or a ptr. To structure.
1). Passing struct members as arguments.
2). Passing struct var. as argument.
3). Passing ptr. To struct as argument.
4). Returning a struct var. from function.
5). Returning a ptr. To struct from function.
6). Passing away of struct as argument.

Note :-
1). All the change made in the away of struct. Inside the called function. Will be visible in the calling function .
2). If the size of a struct is very large then it is not efficient to pass the whole struct. To function.
3) It is better to sent the address of the structure which will improve the execution speed.
4) Passing individual members to a function. Become cumbersome when there are many members & the relationship b/w the members is also last so we can pass the whole structure as an argument
5) As any other van. Structure .van is return the retuned value can be assigned to a structure of the appropriate time.
6) As we pass away to a function away of Structure. Can also be passed to a function where each ele. of away is of Structure type.

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved