473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is the use of a void* pointer?

What is the use of a void* pointer? It points to a thing that is void ?
So does that mean it is undefined or what? and also if someone can guide me on the following?


After each input, the stream extraction operator returns a reference to the stream object that received the extraction message (e.g., cin in the expression cin >> grade). If that reference is used as a condition (e.g., in a while statement's loop-continuation condition), the stream's overloaded void * cast operator function is implicitly invoked to convert the reference into a non-null pointer value or the null pointer based on the success or failure of the last input operation. A non-null pointer converts to the bool value TRue to indicate success and the null pointer converts to the bool value false to indicate failure. When an attempt is made to read past the end of a stream, the stream's overloaded void * cast operator returns the null pointer to indicate end-of-file.
from Dietel fiftth edition C++ How To Program
Aug 8 '09 #1
5 20657
JosAH
11,448 Expert 8TB
A void pointer is a pointer to any type. Such a pointer can be cast to any particulare type and a pointer to any particular type can be cast again to a void pointer.

kind regards,

Jos
Aug 8 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
void* are usually only found in C due to the lack of function overloading.

A void* in C++ is a design weakness since the target type of the cast must be hard-coded which destroys your ability to reuse the code polymorphically.
Aug 8 '09 #3
The type void* is, as JosAH said, a pointer to any type. To be more accurate, it is a pointer to no type at all, as it is just a pointer. In order to use a void pointer, you must use either an explicit or (eech) an implicit cast.

One of the advantages of void pointers are the fact you may cast it to anything. you may treat a void* as an int* on one line and an unsigned char* on the next.

--Troy
Aug 8 '09 #4
donbock
2,426 Expert 2GB
As mentioned in reply #3 by weaknessforcats, the details of how you can use void pointers is different in C and C++. In C there is always an implicit cast to the appropriate target type when an assignment is made to or from a void pointer. This implicit cast does not occur in C++; it is your duty to provide the appropriate explicit cast.

This means that a valid C program that uses void pointers will probably have compiler errors when passed through a C++ compiler.

Personally, I consider this implicit cast one of the most appealing feature of void pointers in my C code.

A void pointer cannot be dereferenced (because the compiler doesn't know what type is being pointed to). This suggests another use for void pointers ... they can be used in a C program to pass opaque object handles to and from a subroutine library. The library API casts object pointers into void pointers before returning them to the caller; and casts input void pointers from the caller into object pointers for subsequent use in the library function. The object type can then be kept hidden from the caller. There are other ways to do this in C (such as an incomplete structure). I'm sure C++ has smarter ways to accomplish this.
Aug 10 '09 #5
donbock
2,426 Expert 2GB
By the way, a void pointer is a data pointer. You ought to keep data and function pointers separate.

For example,
  • Do not assign a function pointer value to a void pointer.
  • Do not use the NULL macro with function pointers.
Aug 10 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
21
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
13
by: ranjeet.gupta | last post by:
Dear All What does exactly below code means struct File { void* data; }; typedef struct File File; typedef File* fl;
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
5
by: Niu Xiao | last post by:
I saw a lot of codes like: void foo(void* arg) void bar(void** arg) f((void*)p) but what does void pointer mean in c? I just know it stands for generic pointer. thanks.
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
6
by: Shraddha | last post by:
What is this exactly... int(*(*ptr (int))(void) First I thought that this is the pointer to function...But I recognize that the syntax iss quite different... If we say that the function is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.