473,322 Members | 1,409 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,322 software developers and data experts.

Should this compile without cast?

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
Jul 22 '05 #1
6 1384

"William Payne" <mi******************@student.liu.se> wrote in message news:bs*********@news.island.liu.se...
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*.

Jul 22 '05 #2

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"William Payne" <mi******************@student.liu.se> wrote in message news:bs*********@news.island.liu.se...
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*.


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
Jul 22 '05 #3

"William Payne" <mi******************@student.liu.se> wrote in message news:bs**********@news.island.liu.se...
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*.

Jul 22 '05 #4

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"William Payne" <mi******************@student.liu.se> wrote in message

news:bs**********@news.island.liu.se...
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*.


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
Jul 22 '05 #5
William Payne wrote:
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.

Jul 22 '05 #6

"Jeff Schwab" <je******@comcast.net> wrote in message news:zb********************@comcast.com...
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

....

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
1
by: Slavisa | last post by:
When I make my program on RedHat 9.0 everything goes fine, but when I try to make on Red Hat 7.2 I get following errors: logic2_msgcln.c:178: warning: initialization makes integer from pointer...
52
by: entropy123 | last post by:
Hey all, I'm working with some legacy C code and I would like to compile it as a CPP file. I get the following error message: driver.cpp:87: cannot convert `void *' to `GenericStruct *' in...
3
by: Eric Newton | last post by:
I'm wondering if there's a compile time cast gaurantee if any? Given the following: //----- begin sample code public interface IRegion { string Text { get; } RectangleF Area { get; } }
3
by: John | last post by:
Hi, I am working on a legacy code. Following is a line of the code which complies with Visual C++.NET 2003 but not with Visual C++ 2005: for ( int i = 0; i < (const)(X.size()); i++ ) {}// X is...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
8
by: lennin | last post by:
#define NULL 0 int main() { unsigned int p = NULL; /* Empty your mind */ if (p!=NULL) delete &p;
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.