C workshop day 7

Embed Size (px)

Citation preview

Technical ChatC-WorkshopDay-7

Syntax-

Function_returntype (*ptr)();EX:void (*ptr)();int (*ptr)();

function_returntype (*ptr)(Datatype);EX: void(*ptr)(int,int);int *(*ptr)(int);

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.

Int *abc(){int a=10;++a;return &a;}void main(){int * ptr; //dangling pointerptr=abc();printf(\n Value of a : %d, *ptr);}

O/p- Value of a: 11(illegal)

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.

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

void abc(){printf(Hello abc);}void main(){void(*ptr)();//function adderssptr=&abc;ptr();// abc();//calling}

O/p- Hello abc