Solved

casting a pointer to unsigned int

Posted on 2003-11-13
7
878 Views
Last Modified: 2012-06-27
I came across something like the foll:

packet_initialize ( (PACKET_HANDLE )p_pkt);

where typedef UINT PACKET_HANDLE;
and struct packet *p_pkt;

I wonder how a pointer to packet can be  cast to an unsigned int

0
Comment
Question by:arut
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9745595
Hi arut,

while it is not the best of the things to do but it is possible to make such casts ... infact on several platforms, a pointer is nothing but an unsigned 32 bit quantity (same as unsigned int!!!)

without the explicit cast, you would get a warning
warning: assignment makes pointer from integer without a cast

with a cast, compiler accepts it


Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9745625
just to add, the cast will compile without a warning if you compile it with -pedantic option

compiler just needs to be sure that you know what you are doing
0
 

Expert Comment

by:pkarth
ID: 9745690
Dear arut,

 The basic concept of Pointers in C is hold a uint value which is expected to be a reference to the memory location.
if you declare a pointer like   void *void_ptr;  it can hold any type of pointer reference. but it should be handled like a hash key. the address of datum which u r storing in that should be referenced using the same type only.

           int int_, *int_ptr;
          FILE *fp1, *fp2;
          void *void_;
           void_ = &int_;
           int_ptr = ( int *) void_;

           void_ = fp1;
           .............
   ..................
           fp2 = ( FILE *)void_;

 like that. in one line to say you can play withe uint value with the pointer to reference. provided dereferencing should be casted to get the exact result. Hope this gives a solution . all the best
-regards
 pkarth
       
           
           
           
0
 
LVL 15

Assisted Solution

by:efn
efn earned 125 total points
ID: 9745739
The definition of C specifies that you can convert a pointer to an integer and an integer to a pointer, but it doesn't guarantee that there will necessarily be any integer type that is large enough to hold all the information in a pointer, so information could be lost in the conversion from pointer to integer.

As to how, in the words of Harbison and Steele, "the pointer is treated as if it were an unsigned integer of a size equal to the size of the pointer. ... If null pointers are not represented as the value 0, then they must be explicitly converted to 0 when converting the null pointer to an integer."  (from "C:  A Reference Manual," 4th ed., 1995)

--efn
0
 
LVL 22

Expert Comment

by:grg99
ID: 9754078
If this machine has UINT type big enough to hold a pointer, this cast will probably "work".  A better question to ask is why the called function expects a UINT when a pointer to a packet would make a  heck of a lot more sense.   There may be some not very good historical or arbitrary design decision lurking in there.  It wouldnt hurt to look into that aspect.  Maybe you can get the design changed.



0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 424
How to creat good software interface ? 1 67
IIS Log files on Exchange 2013 server 6 119
gdb doesn't stop on breakpoint 2 68
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question