• No results found

Passing Address to Functions: The method of passing address to function is called call by reference. In C a function can be called by the call in program in two ways

In document C NOTES (Page 97-101)

Method I: In this method, one structure is written inside another structure

5.10.4 Passing Address to Functions: The method of passing address to function is called call by reference. In C a function can be called by the call in program in two ways

printf("Enter The Number of Integers:\n");

scanf("%d",&n);

printf("Enter The Set of Integers");

for(i=0;i<n;i++) scanf("%i",a+i);

ptr=large;

printf("\nThe Largest Integer is ");

select(a,n,ptr);

printf("\nThe Smallest Integer is ");

select(a,n,small);

}

5.10.4 Passing Address to Functions: The method of passing address to function is called call by reference. In C a function can be called by the call in program in two ways.

1. Call by value 2. Call by reference

1. Call by Value: When a function is called by the calling program, the values to the arguments in the function are supplied by the calling program. The values supplied can be used inside the function. Any alteration to the value inside the function is not accepted in the calling program but the change is locally available in the function. This method is referred as calling a function by value.

Example:

printf("Before Calling a=%d b=%d",a,b);

add10(a,b);

printf("\nAfter Calling a=%d b=%d",a,b);

}

void add10(int x,int y) {

x=x+10;

Prepared by Mr. K. G. Page 98 y=y+10;

printf("\nInside Function a=%d b=%d",x,y);

} their values is available only inside the function and not in the main program (calling program).

Call by Reference: A function can be declared with pointers as its arguments. Such functions are called by the calling program with the address of a variable as argument from it. The addresses of the variables are substituted to the pointers and any alteration to its value inside the function is automatically carried out in that location. The change is indirectly made and is accepted by the calling program. This method is referred as calling a function by reference.

Example:

printf("\nBefore Calling a=%d b=%d",a,b);

add10(&a,&b);

printf("\nAfter Calling a=%d b=%d",a,b);

}

void add10(int *x,int *y) {

*x=*x+10;

*y=*y+10;

printf("Inside Function a=%d b=%d",*x,*y);

}

Output:

Before Calling a=10 b=40 Inside Function a=20 b=50 After Calling a=20 b=50

Note that the increase in values of the arguments a and b is available in the main program also.

Difference between Call by value and Call by reference

Call by value Call by reference

1. This is the usual method to call a function in which only the value of the variable is passed as an argument.

In this method, the address of the variable is passed as an argument.

2. Any alteration in the value of the argument passed is local to the function and is not accepted in the calling program.

Any alteration in the value of the argument passed in accepted in the calling program (since alteration is made indirectly in the memory location using the pointer)

3. Memory location occupied by formal and actual arguments is different.

Memory location occupied by formal and actual arguments is same and there is a saving of memory location.

Prepared by Mr. K. G. Page 99 4. Since a new location is created, this method is

slow.

Since the existing memory location is used through its address, this method is fast.

5. There is no possibility of wrong data manipulation since the arguments are directly used in an expression.

There is possibility of wrong data manipulation since the addresses are used in an expression. A good skill of programming is required here.

Note: If we define an array in a function with auto class, we cannot pass the address of that array back to the main() for subsequent work.

5.10.5 Function Returning Pointers: The function return an int, a float, a double or any other data type, it is also possible to return a pointer.

data type *function-name(argument list);

The above statement functions return an address of specific type of data, which is collected in the pointer variable.

#include<stdio.h>

int *fun();

void main() {

int *x;

x=fun();

printf("%d",*x);

}

int *fun() {

int y=24;

return(&y);

}

5.11 Pointers and Structures: We know that the name of an array stands for the address of its 0th element. The same thing is true for the names of array of structure variables. Suppose product is an array variable of struct type. The name product represents the address of its 0th element. Consider the following declaration:

struct inventory {

char name[30];

int number;

float price;

}product[2],*ptr;

This statement declares product as an array of two elements, each of type struct inventory and ptr as a pointer to data objects of the type struct inventory.

ptr=product;

would assign the address of the 0th element of product to ptr. That is, the pointer ptr will now point to product[0]. Its members can be accessed using the following notation.

ptr->name ptr->number ptr->price

The symbol -> is called the arrow operator and is made up of a minus sign (-) and a greater than sign (>). Note ptr-> is simply another way to writing product[0].

Prepared by Mr. K. G. Page 100 When the pointer ptr is incremented by 1, it is made to point to the next record i.e.

product[1]. The following for statements will print the values of members of all the elements of product array.

for(ptr=product;ptr<product+2;ptr++)

printf(“%s %d %f\n”,ptr->name, ptr->number,ptr->price);

we could also use the notation (*ptr).number

to access the member number. The parentheses around *ptr are necessary because the memory operator “.” has a higher precedence than the operator *.

#include<stdio.h>

struct invent {

char * name[20];

int number;

float price;

};

void main() {

struct invent product[3],*ptr;

printf("Input\n\n");

for(ptr=product;ptr<product+3;ptr++)

scanf("%s%d%f",ptr->name,&ptr->number,&ptr->price);

printf("\nOutput\n\n");

ptr=product;

while(ptr<product+3) {

printf("%s\t%d\t%f\t",ptr->name,&ptr->number,&ptr->price);

ptr++;

} }

While using structure pointers, we should take case of the precedence of operator. The operator ->

and ‘.’ and () and [] enjoy highest priority among the operators. They bind very tightly with their operands. For example, given the definition

struct {

int count;

float *p;

}*ptr;

then the statement

++ptr->count increments count, not ptr. However

(++ptr)->count;

increments ptr first, and then links count. The statement ptr++->count;

is legal and increments ptr after accessing count.

The following statements also behave in the similar fashion.

*ptr->p Fetches whatever p points to.

*ptr->p++ Increments p after accessing whatever it points to.

(*ptr->p)++ Increments whatever p points to.

*ptr++->p Increments ptr after accessing whatever it points to.

Prepared by Mr. K. G. Page 101 5.12 Pointer to Pointer: Suppose if we want to store the address of one pointer variable so we use a variable, which itself pointer (pointer to pointer). Pointer to pointer offers flexibility in handling arrays, passing pointer variables to functions. The general format for declaring a pointer to pointer is:

data_type **ptrvar;

which uses two asterisks (**) symbols placed beside the other. Thus the declaration implies that the variable ptrvar is a pointer to a pointer pointing to a data object of the type data type.

i J k

24 1726 1804

1726 1804 2186

In the above figure the variable k contains the address of j so k declares as a double pointer.

#include<stdio.h>

void main() {

int i=24, *j, **k;

j=&i;

k=&j;

printf("Address of i=%d\n",&i);

printf("Value of j=%d\n",j);

printf("Address of j=%d\n",&j);

printf("Value of k=%d\n",k);

printf("Address of k=%d\n",&k);

printf("Value of j=%d\n",*k);

printf("Value of i=%d\n",*j);

printf("Value of i=%d\n",**k);

}

Output:

Address of i=1726 Value of j=1726 Address of j=1504 Value of k=1504 Address of k=2186 Value of j=1726 Value of i=24 Value of i=24

5.13 void Pointers: Pointers defined to be of a specific data type cannot hold the address of, say, an

In document C NOTES (Page 97-101)