GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 383 of 453    Print  
Difference between void pointer and generic pointer?

  
Total Answers and Comments: 3 Last Update: July 06, 2009     Asked by: srinivasarao 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 31, 2007 05:39:22   #1  
baseersd Member Since: June 2007   Contribution: 34    

RE: Difference between void pointer and generic pointe...

When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.

This is very useful when you want a pointer to point to data of different types at different times.

Here is some code using a void pointer:

#include <stdio.h>
int main()
{
int num[3] {10 20 30};
char name[30] "Welcome to C World";
int *pint NULL;
void *pvoid NULL;
int i;


pint #
for ( i 0; i<3; i++ )
printf(" d " *(pint + i ));
printf("n");

// The same can be done using void pointer as follows.
pvoid #
for ( i 0; i<3; i++ )
printf(" d " *((int *)pvoid + i ));

// Same void pointer can be cast to char.
printf("n");
pvoid name;
for( i 0; i < strlen( name); i++ )
printf(" c" *((char *) pvoid + i));
getch();
}


 
Is this answer useful? Yes | No
October 13, 2008 01:25:35   #2  
pradeep Jadhav Member Since: October 2008   Contribution: 1    

RE: Difference between void pointer and generic pointer?
void pointer can be used to store address of any type of variable e.g.int float char but generic pointer can't.
 
Is this answer useful? Yes | No
July 05, 2009 23:55:42   #3  
abhimanipal Member Since: July 2009   Contribution: 22    

RE: Difference between void pointer and generic pointer?
I just want to make a small point.
When you assign an array to a pointer you can't put an & sign

int arr[5] {1 2 3} *p1;
p1 arr;

This will work


int arr[5] {1 2 3} *p1;
p1 &arr;

error: cannot convert `int (*)[5]' to `int*' in assignment

This is a small mistake in the code segment given in the above example


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape