What is the use of void pointer? When and Why do we make use of void pointers in C programming Language?
Regards
Shivanna
What is the use of void pointer? When and Why do we make use of void pointers in C programming Language?
Regards
Shivanna
void pointers can be assigned to any pointer value. It sometimes necessary to store/copy/move pointers without regard to the type it references.
You cannot dereference a void pointer.
Functions such as malloc, free, and scanf utilize void pointers.
So one must be careful while using void pointer
how can i use void pointer?can u give examples?
my program gives error like this.
error C2036: 'void *' : unknown size
error C2440: 'static_cast' : cannot convert from 'void' to 'int'
Hi, you can assign other pointer types(including void pointer) to a void pointer without an explicit cast(converse is not true though).
If not directly, a void pointer can be dereferenced indirectly by casting it to the appropriate type. For ex:
int val = 6;
void *v;
v= &val;
printf("%d", *(int *)v); // would give you the output as 6.
char *s = "abc";
void *v2;
v2= s;
printf("%s", (char *)v2); // would give you the output as "abc".
Void is a non-type which has no size, no format, it is a black hole from which you cannot read. void * is pointer-to-void. You would have assigned or read or retrieved some values pointing to nothing. Only if your post your code I could give a more elaborate and exact answer.
You can't compare pointers of different types. eg. You cant compare an int and char pointer.
In this case, you can declare one pointer as void and do comparision.
thanks for your attendance.
i want to use void * like templates(to do my homework).
first i have to assign void * to char *.then compare them byte to byte.
but i couldn't do this.i don't know its syntax.
can you know how to do this?
this is my hw.
Last edited by kingalmora; 12-23-2006 at 11:03 AM.
Friends is void pointer different from null pointer if so what is the difference.Can someone explain with some example.
A void pointer is a pointer which can point to any data type (which of course requires typecasting). Where as a null pointer is used to indicate that it is pointing nowhere.
A null pointer is a value that any pointer may take to represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points to.
Just to add that we cannot do pointer arithmetic on Void Pointers.
I think void pointer is used in 'vtable' inorder to avoid the confilct of the linker in run time binding.
Hi, void pointer is a type of pointer for what u can decide them to poin what u want.
hey first of all every buddy need to understand the need of defining the type of any pointer
defining the type of any pointer tells compiler how much memory(how many bytes) to be accessed while appling some operation to it.
eg.
int var = 10;
int *ptr = &var;
now compiler knows if value is to be read through ptr then 4 bytes to be read and tell.
now in case of void type of pointer compiler never knows how many bytes to be accessed.
so inorder access it we need to inform compiler about its type or how many bytes to be accessed.