Can you dereference variables?

Started by
8 comments, last by Promit 21 years, 11 months ago
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
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
No.

That should give you a compiler error.

You can only dereference pointers.

C should also complain for the same reason.

You can''t store an address in an int without using reinterpret_cast. And no, you can''t dereference an int, as it isn''t referencing anything.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
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.
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!)
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
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

quote:Original post by Ready4Dis
address is a long, might as well use it this way

You probably mean that an address takes the same amount of storage as a long. The C and C++ Standards impose no such restriction.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
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.
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!)
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!)

This topic is closed to new replies.

Advertisement