12 August 2015

Best Pointers With Function Pointer Function And Function Return Pointers In C Tips You Will Read This Year

Image

Hi friend, I have come with next topic pointer with function in C.Function in C programming language  is a basic step for modular design coding.Function in C is a block of independent code which makes C programming in the module and make debugging is easy.So without any spending time, I come to the detailed study of pointers with function in C programming language.In this post, I  will also cover pointer to function and function return function with example.



Definition:

A function is a self-contained block of statement that perform a coherent task of some kind.
you can read about basic of functions in C is here.

Function has some property:


  1. recursion. 
  2. Calling a function by value or reference. 
  3. return type of function

pointers are used with function in two way:

Pointers as we earlier studied, it used with function also.

1.calling function by reference
2.pointer to function


Passing argument to function with pointer helps programmer

A.A function can't return two values at single time.So we use a pointer to returns more than two values.In this argument, we make change in pointer value and this changed the passed argument actual values.
We can understand it as
Function call by reference as 


fun(&a,&b); //it is called function call
int fun(int *a,int *b){ //function definition with pointers argument
*a=2;                       //pointer inilization
*b=3;
}                              //*a=2 means *(&a)=2 i.e a=2 because printing a variable or *(&a) is same.i.e a=*(&a).
Therefore function call by reference show changed value if argument values change in calling function.Due to this we can returns more than one value from function indirectly.
B.If you that the value of actual argument should not get changed in the function being called pass the actual argument by value.like fun(a,b);
Otherwise by reference.

Pointer to function 

as pointer hold address of function.It is declared as
void (*fun)();
It store address of function as
fun=display;//display is a functions.Which is declared as shown in picture
Called to the stored function as
fun();or (*fun)();
If you want to find function address then write as printf("%u",fun);
Note- when you write function definition before main() function then no need to declare a prototype of the function.It is used as you can call any function as shown in photo

Function Pointer In C


Prototype of function declared as

return type function_name(argument data type variable_name,argument data type variable_name)
Note-variable_name is option here.i.e you can write void fun(int,int); but in function definition datatype with variable name is must.

Function can also return pointer

int *fun(){ //function which will return address of variable.
static int i=20;
return(&i);
}
int main(){
int *p;//so that p hold return function address
p=fun();
return 0;
}

function return pointer

It is used in handling string which I will post in the upcoming post.

Note: Array treated as a pointer, I will discuss it in detail in next post.
Call by value and call by reference example 


No comments:

Post a Comment

Ads Inside Post

Contributors