Should this compile without cast?

W

William Payne

In the following code, should the code compile without casting &n to void* ?
My compiler accepts it, but should it?

void foo(void*)
{
}

struct bar
{
int n;
};

int main()
{
bar inst;

foo(&inst); /* Does standard C++ require a void* cast here? */
}

/ William Payne
 
R

Ron Natalie

William Payne said:
foo(&inst); /* Does standard C++ require a void* cast here? */

No, any object pointer can be implicitly converted to void*.
The reverse is not true.

Note that pointers to any sort of member or function are not
convertible to void*.
 
W

William Payne

Ron Natalie said:
No, any object pointer can be implicitly converted to void*.
The reverse is not true.

Note that pointers to any sort of member or function are not
convertible to void*.

Thank you, Ron. I noticed in some code in a book that a pointer to a struct
was being cast to char* in a call to a function that was expecting a void*.
I changed from char*-cast to void*-cast and both worked and then I removed
the cast altogether and it still worked. And I mean works as no compile-time
or run-time errors. Just wanted to make sure I wasn't relying on some
dubious behaviour allowed by my compiler. Wonder why the author of the book
saw fit to cast to char* though...

/ William Payne
 
R

Ron Natalie

William Payne said:
Wonder why the author of the book
saw fit to cast to char* though...

Don't know. Can you type the example in? What was the book?
The only thing I can think of is if the char* cast was applied not to
a pointer, but to an object that had a conversion operator for char*.
 
W

William Payne

Ron Natalie said:
Don't know. Can you type the example in? What was the book?
The only thing I can think of is if the char* cast was applied not to
a pointer, but to an object that had a conversion operator for char*.

Well, I could type it in, but I would probably get criticism for posting
that code that calls non-standard api-functions. It was from Charles
Petzold's book Programming Windows, fifth edition.

/ William Payne
 
J

Jeff Schwab

William said:
Thank you, Ron. I noticed in some code in a book that a pointer to a struct
was being cast to char* in a call to a function that was expecting a void*.
I changed from char*-cast to void*-cast and both worked and then I removed
the cast altogether and it still worked. And I mean works as no compile-time
or run-time errors. Just wanted to make sure I wasn't relying on some
dubious behaviour allowed by my compiler. Wonder why the author of the book
saw fit to cast to char* though...

/ William Payne

Way, way back in time, before the C programming language had a "void*"
type, it was common to use "char*" as a generic sort of pointer.
 
R

Ron Natalie

Jeff Schwab said:
Way, way back in time, before the C programming language had a "void*"
type, it was common to use "char*" as a generic sort of pointer.

Hey, I resemble those remarks. Actually, back before C has void*, it was common
to use "int" as a generic sort of pointer.

#define PS 0177776

struct {
int integ;
};

PS->integ

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top