473,804 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type cast and pointer

ra
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....

I need a DWORD or INT or FLOAT .....

How are the best way to cast a pointer?
Thansk!
Sep 11 '05 #1
4 5599
"ra" <ro**********@g mail.com> wrote in message
news:dg******** **@domitilla.ai oe.org...
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....
DWORD is a Windows-specific typedef, your question does not belong in this
forum about standard C++.
And you should provide the exact text of the warning you are confronted
with.

The point of the warning is probably that, for a pointer-to-integer cast to
be reversible (i.e. can be casted back to the same pointer value), you need
to make sure that you use an integer type that has sufficient accuracy.
Actually, it is not guaranteed that such an integer type exists.
But on your platform, someting like INT_PTR might be a typedef to the
integral type you are looking for.
I need a DWORD or INT or FLOAT .....

How are the best way to cast a pointer?


You may also want to read about C++-style type casts: reinterpret_cas t,
static_cast, etc.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Sep 11 '05 #2
ra wrote:
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;
Well the obvious question is, why? Newbies often seem to have a fatal
attraction towards doing obscure and dangerous things in C++.

My compiler (V.S. dot Net 2005) return a warning....
Not surprising when you do dangerous things.

I need a DWORD or INT or FLOAT .....

Need for what? This is hard to understand because you haven't provided
the context.
How are the best way to cast a pointer?

I don't think you need to cast a pointer. I think you need to explain
what you are trying to do and then I can (hopefully) explain how to do
it without you having to cast any pointers (or at least how to cast them
without warning messages).

Thansk!


john
Sep 11 '05 #3
You can do this:

DWORD d = reinterpret_cas t<DWORD&>(p);

But two words of warning:

- this invokes undefined behaviour, it works for Visual C++ .net 2005
and 2003 and the behaviour IS defined on that compiler and platform
assuming you are targeting 32-bit IA32. Regarding C++, the behaviour,
however, is not defined.

You might be better off doing two things:

First, use type other than DWORD so that your code keeps working on
64-bit Windows binaries. I approach similiar problem differently but
not going into details as that is off-topic. But just think of some
other type than DWORD (which works fine as of now on tools and platform
you currently targeting-- assuming 32-bit Windows).

Second, you might want to consider doing pointer aritchmetic instead of
casting the bits to represent integer value instead of memory address.

Something akin to this:

typep* p; // initialized somewhere..

char* base = 0;
ptrdiff_t ip = reinterpret_cas t<char*>(p) - base;

In this case, ip would pretty much be the linear address stored into
integer-- it's better to use this than reinterpret_cas t the bits and
assume it works, because it doesn't, I can give example of a fairly
common compiler where the former approach will produce catastrophic
failure if you want to dwell that path.

Thank you for your time and good luck!

Sep 11 '05 #4
ra wrote:
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....

I need a DWORD or INT or FLOAT .....


DWORD is not necessarily big enough to hold a pointer (it
isn't on WIN64). You need a DWORD_PTR, which despite the
stupid name isn't a pointer to a DWORD, but another integral
type bigenough to hold either a PTR (casted) or a DWORD.

Sep 12 '05 #5

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

Similar topics

4
3484
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
5
8733
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to use it with. Now, if that T happens to be some subclass of a known base class (object, in this case), I want to perform some extra stuff ... I've read Faq#35, and the most natural solution would have
13
2345
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
4
14135
by: NotYetaNurd | last post by:
Hi all, I read that delegates are type safe and Functional pointers are not!!... in this context what does type safety mean? can anyone of you throw some light on it regards, ...
15
2303
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int) x; is equivalent to int t = x; (could the latter produce a warning on some complier?) 3. I know that implicit conversions take place with function arguments, but am a bit shaky here. I suppose that passing a char to a function accepting
15
9358
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main': chris_readle_project3_assignment3.c:23: warning: passing arg 1 of `displaySales' from incompatible pointer type And here is the code in question:
51
4574
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 there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
31
3201
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
7
5292
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *' translator1.c(1703) : warning C4152: nonstandard extension, function/ data pointer conversion in expression whenever i cast a function pointer to a void* or back. Is there any reason for this warning ? Looks like good C code for me.
7
3953
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.