1) Function pointers can be declared to return different data types like void, int, pointers etc. and can accept parameters.
2) Functions can return values by returning the value directly or by returning the address of a variable. Returning the address is called return by address.
3) If a function does not return anything, its return type is specified as void. If it returns an int value, the return type is int. If it returns a pointer to an int, the return type is int*.
3. return by value, return by address:
whenever a function is returning value type data then it is called
return by value, i.e. function returning value type.
Whenever a function is returning address type data then it is called
return by address, i.e. function returning pointer.
In implementation whenever a function is not returning the value,
then specify the return type as void.
Whenever a function is returning an int variable then specify the
return type as an int, i.e. function returning a value.
In implementation whenever a function returns a int variable
address then specify the return type as an int*, i.e. function
returning pointer called return by address.
4. Int *abc()
{
int a=10;
++a;
return &a;
}
void main()
{
int * ptr; //dangling pointer
ptr=abc();
printf(n Value of a : %d, *ptr);
}
O/p- Value of a: 11(illegal)
5. according to storage class the life time of the auto variable
is restricted within the body that's why in above program,
when the control will pass back to main() that's variable is
destroyed but still the pointer is pointing to inactive
location.
The solution of the dangling pointer is inplace of creating
aut, recommended to create static variable.
6. int * abc()
{
static int a=10;
++a;
return &a;
}
void main()
{
int *ptr;
ptr=abc();
printf(n value of a : %d, *ptr);
}
O/p-
value of a : 11