Archived

This topic is now archived and is closed to further replies.

Promit

Can you dereference variables?

Recommended Posts

Promit    13246
Variables meaning those not declared as pointers. This obviously has no useful application, but basically, will this work out at all:
  
char someChar = ''0'';
int addr = 0;
char otherChar = ''\0'';

int main()
{
addr = &someChar;
//Can I access someChar by dereferencing addr, even tho it isn''t a pointer?

otherChar = *addr;
}
  
Just curious...will the compiler complain? Will it work only in C and not C++? (wait, I don''t remember if C is strongly typed or not, damnit) ____________________________________________________________ Direct3D vs. OpenGL

Share this post


Link to post
Share on other sites
SteveC    122
No.

That should give you a compiler error.

You can only dereference pointers.

C should also complain for the same reason.

Share this post


Link to post
Share on other sites
Sandman    2210
Also, if you did store a pointer value in an int for whatever reason, and then cast it back to a pointer later on, the results would be rather unpredictable due to the different sizes of pointers and ints on different compilers and platforms.

Share this post


Link to post
Share on other sites
Ready4Dis    180
I beleive you''re trying to do this:

char someChar = ''0'';
unsigned long addr = 0; //An address is a long, might as well use it this way
char otherChar = 0;

int main()
{
addr = (long)&someChar;
otherChar = *(char*)addr;
}

Note, this hasn''t been tested, but it should work... (on a 32-bit compiler anyways, if it was a 16-bit compiler, you''d need to use a (char far*) instead of (char*)).

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

Share this post


Link to post
Share on other sites
davepermen    1047
quote:
Original post by Promit
Just curious...will the compiler complain? Will it work only in C and not C++? (wait, I don''t remember if C is strongly typed or not, damnit)

____________________________________________________________
Direct3D vs. OpenGL



just curious, why did you not tested? create main.cpp and compile
create main.c and compile..
you''ll see the errors..


"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
a person    118
but all computers with 32bit addressing will use 32bits for storing addresses. so this would only work on pcs with 32bit addressing (ie not an x86 in realmode, I-64, palms, many console systems, etc). but it will work on all x86 pcs running in protected mode.

Share this post


Link to post
Share on other sites
Ready4Dis    180
SabreMan: Yes, which is why I said... well on 32-bit compilers! If you try putting this through a 64-bit compiler (or even a 32-bit compiler that uses 64-bit addressing, or 16-bit addressing, it won''t work).

daveperman: If you add a return for the int main() it gives ZERO errors and ZERO warnings... just tested it, happy now?

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

Share this post


Link to post
Share on other sites
Ready4Dis    180
a person: If you use a 16-bit compiler like Turbo C/C++ for Dos, you''d have to use a char far* instead of a char. The addresses are still 32-bit. If there was a 16-bit address, using a long wouldn''t hurt, you''re just wasting 2bytes of the 4...

WORKING turbo C/C++ v3.0 for dos (16-bit real-mode program!) Notice, the ONLY change I had to make was to tell it to use far addressing instead of the defualt (near), you can compile this under msvc (but in 32-bit pmode, it uses far by defualt).

char someChar = ''0'';
unsigned long addr = 0; //An address is a long, might as well use it this way
char otherChar = 0;

int main()
{
addr = (long)&someChar;
otherChar = *(char far*)addr;
return 0;
}

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

Share this post


Link to post
Share on other sites