Void Pointers
- Before taking a deep dive into what are Void Pointers ,WE Suggest you to
please go through to this Topics : What are Pointers?
- What are Void Pointers ?
- As you know Pointers are the variable which stores the address of an another variable,which is nothing but location of that variable
- As You know , Pointers can only store the address of variable only when their data types are same.
- For example:int type of pointer can only store address of integer type of variable.
- Float type of pointer can only store address of float type of variable And so on.
- By over coming from the above functionality, Void Pointer Comes in Action.
Void Pointers
- Void Pointers are the pointer Which can store the address of any kind of data type.
- Just while assigning and Accessing the Pointer or variable which is point by the pointer you have to do type casting.
- Let's learn how to declare , initialize , and type casting of an Void pointers.
Syntax: Declaration of void Pointer
void * Pointer_name;
void * Pointer_name;
Let's declare an pointer variables:
#include<stdio.h>
void main()
{
void *p1; //declaration of void pointer
void *p2; //declaration of void pointer
void *p3; //declaration of void pointer
}
#include<stdio.h> void main() { void *p1; //declaration of void pointer void *p2; //declaration of void pointer void *p3; //declaration of void pointer }
Method of Initializing the void pointer will remain same:
#include<stdio.h>
void main()
{
int a = 10;
void *p1; //declaration of void pointer
//initializing the void pointer
p1 = &a; //assigning the address of a variable to
//void pointer p1.
}
#include<stdio.h> void main() { int a = 10; void *p1; //declaration of void pointer //initializing the void pointer p1 = &a; //assigning the address of a variable to //void pointer p1. }
Syntax of Accessing the data which is Pointed by the Pointer:
*(datatype_of_stored_variable *)pointer_name;
*(datatype_of_stored_variable *)pointer_name;
Let's Access the data which is point by the pointer:
#include<stdio.h>
void main()
{
int a = 10;
void *p1; //declaration of void pointer
//initializing the void pointer
p1 = &a; //assigning the address of a variable to
//void pointer p1.
value = *(int *)p1 // storing the value which is point
//by the pointer p1 to variable value
printf("\nData which is point by the pointer p1 is %d",value);
}
#include<stdio.h> void main() { int a = 10; void *p1; //declaration of void pointer //initializing the void pointer p1 = &a; //assigning the address of a variable to //void pointer p1. value = *(int *)p1 // storing the value which is point //by the pointer p1 to variable value printf("\nData which is point by the pointer p1 is %d",value); }
Output
Data which is point by the pointer p1 is 10.
Now we will store and access the address and value of float type of variable.:
#include<stdio.h>
void main()
{
float a = 7.1;
void *p2; //declaration of void pointer
//initializing the void pointer
p2 = &a; //assigning the address of a variable to
//void pointer p2.
value = *(float *)p2 // storing the value which is point
//by the pointer p2 to variable value
printf("\nData which is point by the pointer p2 is %f",value);
}
#include<stdio.h> void main() { float a = 7.1; void *p2; //declaration of void pointer //initializing the void pointer p2 = &a; //assigning the address of a variable to //void pointer p2. value = *(float *)p2 // storing the value which is point //by the pointer p2 to variable value printf("\nData which is point by the pointer p2 is %f",value); }
Output
Data which is point by the pointer p2 is 7.1.
Now we will store and access the address and value of char type of variable.:
#include<stdio.h>
void main()
{
char c = 'A';
void *p3; //declaration of void pointer
//initializing the void pointer
p3 = &a //assigning the address of c variable to
//void pointer p3.
value = *(char *)p3 // storing the value which is point
//by the pointer p3 to variable 'value'
printf("\nData which is point by the pointer p3 is %c",value);
}
#include<stdio.h> void main() { char c = 'A'; void *p3; //declaration of void pointer //initializing the void pointer p3 = &a //assigning the address of c variable to //void pointer p3. value = *(char *)p3 // storing the value which is point //by the pointer p3 to variable 'value' printf("\nData which is point by the pointer p3 is %c",value); }
Output
Data which is point by the pointer p3 is A.
Simple program: Using above concept
#include<stdio.h>
void main()
{
int result1;
float result2;
int a = 10,b=30;
float c = 7.5,d = 9.5;
void *p1,*p2,*p3,*p4; //declaring the void pointers.
p1 = &a //assigning the address to
p2 = &b; //pointer variables
p3 = &c;
p4 = &d;
result1 = *(int *)p1 + *(int *)p2; //catch1
result2 = *(float *)p3 + *(float *)p4; //catch2
printf("\nSum of two variable a + b = %d",result1);
printf("\nSum of two variable c + d = %f",result2);
}
#include<stdio.h> void main() { int result1; float result2; int a = 10,b=30; float c = 7.5,d = 9.5; void *p1,*p2,*p3,*p4; //declaring the void pointers. p1 = &a //assigning the address to p2 = &b; //pointer variables p3 = &c; p4 = &d; result1 = *(int *)p1 + *(int *)p2; //catch1 result2 = *(float *)p3 + *(float *)p4; //catch2 printf("\nSum of two variable a + b = %d",result1); printf("\nSum of two variable c + d = %f",result2); }
Output
Sum of two variable a + b = 40
Sum of two variable c + d = 17.00000.
Sum of two variable c + d = 17.00000.
- This is how void pointer works.
- Void pointer becomes pretty much complicated at the time of accessing the data.
- There is no harm for using the normal pointers ,Just it is a Good thing that you know what are void Pointers, as in many interviews this question is asked that what are Void pointers.
- So , it is good thing to know what are void pointers.
- So, do more practice for having a good command on void pointers.
Further Concept:
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
Further Topics:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment