• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Dangling & Function pointers in C

About Dangling pointer & Function pointer in C

Dangling pointer :-

Which pointer variable pointing to a inactive or dead location, it is called dangling pointer.

Function pointer :

Which pointer variable holds the address of function, it is called function pointer

Advantage :-

By using function pointers in C, we can pass a f’n as a argument to another function

 

  • By using function pointers in C, it is possible to access or modify the local data of a function from outside.
  • function pointer call 2 are faster than normal f’n call

syntax :  datatype*(*ptr)(); – non parameterized

ex: int*(*ptr)();

datatype*(*ptr)(datatype);  – > parameterized

ex : int*(*ptr)(int);

  • Depends on the function pointer head only, we require to decide the parameterized or not parameterized function.

  

 

Written by Call by value , written by address :

  • Whenever of fun’s is returning value type data then it is called “written by value”
  • Whenever a function is returning address type data then it is called written by address, i.e function returning pointer type.
  • The basic advantage of return by address is we can modify or access local data type of a function from outside.
  • Whenever a function is returning an integer value the specify the return by address is we can modify or access local data of function from outside.
  • Whenever a function is returning an integer value then specify the return type as an into i.e function
  • The basic function advantage of return by address is we can modify or access local data of a function from outside
  • Whenever a function is returning an integer value then specify the return type as an int. i.e, function retuning value(return by value ).
  • Whenever a function is returning an integer variable address ( is we can modi) then specify the return type as an int

i.e, ( function returning pointer (return by address)

 

int *abc()

{

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(illegal)

 

  • According to storage classes of c, a is a auto variable which is constructed with in the abc() function so when the control will pass to main() function it will destroyed
  • In above program ptr is called dangling pointer because it is pointing to inactive or dead location
  • The solution of the dangling pointer is in place of creating auto reconsidered to create static variable because lifetime of the static variable is entire the program.

 

int *xyz()

{

static int s=5;

–s;

return &s;

}

void main()

{

int *ptr

ptr=xyz();

print=xyz();

printf(“\n static data: %d”,*ptr);

o/p : static data : 4

 

void abc()

{

printf(“welcome abc”);

}

void main()

{

void (*ptr)();       // function pointer

ptr=&abc;

ptr(); //abc();

}

o/p : welcome abc

 

void abc()

{

printf(“welcome abc”);

}

void main()

{

void(*ptr)();

ptr=&abc;

abc();

}

o/p : welcome abc

 

int xyz (void)

{

int a=10;

++a;

return a;

}

 

void main()

{

int(*ptr)(void);

int x;

ptr=&xyz;

x=ptr();

printf(“value of : %d”,x);

}

o/p : value of x : 11

 

int abc()

{

static int s=15;

++s;

return &s;

}

}

void main()

{

int*(*ptr)();         // function pointer

int *p;        //      pointer integer

ptr=&abc;

p=ptr();

int abc(int x)

{

static  int s;

s=++x;

return &s;

}

void xyz(int i)

{

printf(“\n static data is xyz : %d”,i);

}

void fun ptr(int * myptr)

{

myptr= 88;

}

void main()

{

int *(*ptr) (int);

int +*p;

int +a;

ptr=&abc;

p=ptr(10);

xyz(ptr(20));

funptr(ptr(30));

a=*p;

ptrinf(“\n static data in main : %d”,a);

static data in main 88.

 

35

 

 

 

  • Where ever the function is not returning only values then specify the return type as void.
  • void means nothing i.e, return value

 

void abc()

{

printf(“Hello abc”);

}

void main()

{

abc();

}

o/p : Hello abc

 

  • If the function return type is void then it is possible to place empty return statement.
  • From void() function when we are placing empty return statement then control will pass to back to the calling

 

36

 

void tex(long int no)

{

switch(no)

{

case 1 : printf (“one”);

break;

case 2 : printf (“two”);

break;

case 3 : printf (“three”);

break;

 

case 4 : printf (“four”);

break;

case 5 : printf (“five”);

break;

case 6 : printf (“six”);

break;

case 7 : printf (“seven”);

break;

case 8 : printf (“eight”);

break;

case 9 : printf (“nine”);

break;

case 10 : printf (“ten”);

break;

case 11 : printf (“eleven”);

break;

case 12 : printf (“twelve”);

break;

case 13 : printf (“thirteen”);

break;

case 14 : printf (“fourteen”);

break;

case 15 : printf (“fifteen”);

break;

case 16 : printf (“sixteen”);

break;

case 17 : printf (“seventeen”);

break;

case 18 : printf (“eighteen”);

break;

case 19 : printf (“nineteen”);

break;

case 20 : printf (“twenty”);

break;

case 30 : printf (“thirty”);

break;

case 40 : printf (“forty”);

break;

case 50 : printf (“fifty”);

break;

case 60 : printf (“sixty”);

break;

case 70 : printf (“seventy”);

break;

case 80 : printf (“eighty”);

break;

case 90 : printf (“ninety”);

break;

case 100 : printf (“hundred”);

break;

case 1000 : printf (“Thousand”);

break;

case 1000000 : printf (“Lakh”);

break;

case 10000000 : printf (“Crore”);

break;

}// end of switch

 

} // end of text.

 

void digittotx(long int n)

{

long int t;

if(n==0)

printf(“Zero”);

if(n>=10000000)

{

t=n/10000000;

if(t<=20)

text(t)

else

text(10000);

text(t%10);

}

 

text(10000000);

n=n%10000000;

}

if(n>=1—–)

{

t=n/100000;

if(t<=20)

{

text(t);

else

{

text(t/10*10);

text(t%10);

}

text(100000);

}

if(n>=1000)

{

t=n/1000;

if(t<=20)

text(t);

else

{

text(t/10*10);

text(t%10);

}

text(1000);

n=n%1000;

}

if(n>=100)

{

t=n/100;

text(t);

text(100);

n=n%100;

}

if(n<=20)

text(n);

else

{

text(n/10*10);

text(n%10);

}

 

void currency (long double rps)

{

long int r,p;

r=(long int)rps;

digittotxt(r);

printf(“Rupees“);

p=(rps-r)*100;

digittotxt(p);

printf(“paise”);

}

 

void main()

{

long double n;

clrscr();

printf(“Enter amount:”);

scanf(“%LF”,&n);

currency(n);

getch();

 

Summary
Review Date
Reviewed Item
Dangling & Function pointers in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Dangling & Function pointers in C"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.