Right Click to Search

Wednesday, January 6, 2025

De-Referencing Void Pointers in C Programming Language


Void Pointers are -
  • General Purpose 
  • Stores the address of any type of variable
Why to use Void Pointers ?

float *ptr;
int num;
ptr = &num ;  // Illegal Use of Pointer 

So in order to increase the re-usability of the Pointer it is declared as void Pointer.



De-Referencing void Pointer :
Suppose 
  • ptr is Void Pointer 
  • inum is integer
  • cnum is character
  • fnum is float

Whose Address is stored in Void Pointer ?

How to De-Reference it ?
Integer
*((int*)ptr)
Charcter
*((char*)ptr)
Floating
*((float*)ptr)

#include
main()
{
int inum = 8 , temp ;
float fnum = 67.7;
void *ptr;
ptr = &inum;    // Assign address of integer to void pointer
temp = *((int*)ptr); 
 
printf("\nThe valu of inum = ",temp);
}

Output :
8




Bookmark & Share

Tags / Keywords : |

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates