SlideShare a Scribd company logo
1 of 13
Generic coding with void *
02/22/17
By: Sandip Moradiya
1
Suppose we have to declare integer pointer,character
pointer and float pointer then we need to declare 3 pointer
variables.
Instead of declaring different types of pointer
variable it is feasible to declare single pointer variable
which can act as integer pointer,character pointer.
void pointer (generic pointer)
2
Basic of void pointer
 In C General Purpose Pointer is called as void Pointer.
 It can store address of any type of variable
 The compiler has no idea what type of object a void Pointer
really points to ?
 Memory required for void pointer is 8 byte
 Void pointer increment by 1 byte
 It does not have any data type associated with it
3
Declaration of Void Pointer
void *pointer_name;
Void Pointer Example :
void *ptr; // ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
ptr = &cnum; // ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum; // ptr has address of float data
4
Dereferencing void pointer
We cannot directly dereference void pointer
like,
int intData=10;
void *ptr;
ptr=&intData;
printf(“%d”,*ptr);
// output: compiler warning/error
Type casting of void pointer is needed
printf(“%d”,*(int *)ptr);
//output: 10
5
void pointer reference to array
char string[]={“Hello World"};
void *ptr;
ptr=&string[0];
printf(“data : %sn”, (char*)ptr);
//output: data : Hello World
int data[]={1,2,3,4,5,6};
void *ptr=&data[0];
for(int i = 0; i < 6; i++)
{
printf(“%d ”,ptr);
ptr = ptr + 4; // pointing to next element in array
}
//output: 1 2 3 4 5 6 6
void pointer dereference from array
char string[]={“Hello world"};
void *ptr;
ptr=&string[0];
int data[]={1,2,3,4,5,6};
void *ptr=&data[0];
for(int i = 0; i < strlen(string); i++)
printf("%c",*(char *)ptr++);
//output : Hello world
for(int i = 0; i < 6; i++)
{
printf("%d ",*(int *)ptr);
ptr=ptr+4;
}
//output : 1 2 3 4 5 6
7
void pointer reference to structure
struct structure
{
int num;
char c;
}stData;
struct structure data;
void *ptr;
ptr = &data;
8
void pointer dereference from structure
struct structure data;
void *ptr;
ptr = &data;
data.num=10;
data.c='a';
printf("%d",*(int *)ptr);
//output: 10
s1 = s1 + 4;
printf("%c",*(char *)ptr);
// output: a
struct structure
{
int num;
char c;
}stData;
9
Array of void pointer
int data1 = 0, data2 = 30, data3=12;
void *ptr[3];
ptr[0]=&data1;
ptr[1]=&data2;
ptr[2]=&data3;
for(int i = 0; i < 3; i++)
printf("%dn",*(int *)p[i]);
//output: 0
30
12
10
void pointer as parameter of function
int num(void * vPtrData)
{
int result=*(int *) vPtrData;
return result;
}
int data=10;
void *ptr=&data;
printf("%dn",num(ptr));
//output: 10
11
Void function pointer
void *num(int pdata)
{
void *result=&pdata;
return result;
}
int data=10;
void *ptr=&data;
printf("%dn", *(int *)num(data));
//output: 110
12
Thank you
13

More Related Content

What's hot

What's hot (20)

Pointers in c
Pointers in cPointers in c
Pointers in c
 
File in C language
File in C languageFile in C language
File in C language
 
Type conversion
Type  conversionType  conversion
Type conversion
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
structure and union
structure and unionstructure and union
structure and union
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
C pointers
C pointersC pointers
C pointers
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
pointers
pointerspointers
pointers
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 

Similar to Void pointer in c (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Types of pointer
Types of pointerTypes of pointer
Types of pointer
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Pointers Notes.pptx
Pointers Notes.pptxPointers Notes.pptx
Pointers Notes.pptx
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Ponters
PontersPonters
Ponters
 
Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
CPointer-hk.ppt
CPointer-hk.pptCPointer-hk.ppt
CPointer-hk.ppt
 

Recently uploaded

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 

Recently uploaded (20)

Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

Void pointer in c

  • 1. Generic coding with void * 02/22/17 By: Sandip Moradiya 1
  • 2. Suppose we have to declare integer pointer,character pointer and float pointer then we need to declare 3 pointer variables. Instead of declaring different types of pointer variable it is feasible to declare single pointer variable which can act as integer pointer,character pointer. void pointer (generic pointer) 2
  • 3. Basic of void pointer  In C General Purpose Pointer is called as void Pointer.  It can store address of any type of variable  The compiler has no idea what type of object a void Pointer really points to ?  Memory required for void pointer is 8 byte  Void pointer increment by 1 byte  It does not have any data type associated with it 3
  • 4. Declaration of Void Pointer void *pointer_name; Void Pointer Example : void *ptr; // ptr is declared as Void pointer char cnum; int inum; float fnum; ptr = &cnum; // ptr has address of character data ptr = &inum; // ptr has address of integer data ptr = &fnum; // ptr has address of float data 4
  • 5. Dereferencing void pointer We cannot directly dereference void pointer like, int intData=10; void *ptr; ptr=&intData; printf(“%d”,*ptr); // output: compiler warning/error Type casting of void pointer is needed printf(“%d”,*(int *)ptr); //output: 10 5
  • 6. void pointer reference to array char string[]={“Hello World"}; void *ptr; ptr=&string[0]; printf(“data : %sn”, (char*)ptr); //output: data : Hello World int data[]={1,2,3,4,5,6}; void *ptr=&data[0]; for(int i = 0; i < 6; i++) { printf(“%d ”,ptr); ptr = ptr + 4; // pointing to next element in array } //output: 1 2 3 4 5 6 6
  • 7. void pointer dereference from array char string[]={“Hello world"}; void *ptr; ptr=&string[0]; int data[]={1,2,3,4,5,6}; void *ptr=&data[0]; for(int i = 0; i < strlen(string); i++) printf("%c",*(char *)ptr++); //output : Hello world for(int i = 0; i < 6; i++) { printf("%d ",*(int *)ptr); ptr=ptr+4; } //output : 1 2 3 4 5 6 7
  • 8. void pointer reference to structure struct structure { int num; char c; }stData; struct structure data; void *ptr; ptr = &data; 8
  • 9. void pointer dereference from structure struct structure data; void *ptr; ptr = &data; data.num=10; data.c='a'; printf("%d",*(int *)ptr); //output: 10 s1 = s1 + 4; printf("%c",*(char *)ptr); // output: a struct structure { int num; char c; }stData; 9
  • 10. Array of void pointer int data1 = 0, data2 = 30, data3=12; void *ptr[3]; ptr[0]=&data1; ptr[1]=&data2; ptr[2]=&data3; for(int i = 0; i < 3; i++) printf("%dn",*(int *)p[i]); //output: 0 30 12 10
  • 11. void pointer as parameter of function int num(void * vPtrData) { int result=*(int *) vPtrData; return result; } int data=10; void *ptr=&data; printf("%dn",num(ptr)); //output: 10 11
  • 12. Void function pointer void *num(int pdata) { void *result=&pdata; return result; } int data=10; void *ptr=&data; printf("%dn", *(int *)num(data)); //output: 110 12