Valhalla Legends Forums Archive | C/C++ Programming | Simple C++ Question

AuthorMessageTime
TheMinistered
Let me show you a few code samples that retain to the question first:
[code]
void* ptrBuffer = 0;
const char DataBuffer[] = "Hello World!";
[/code]

how come you can do this without the & (adress of) operator:
[code]
ptrBuffer = (void*)DataBuffer;
[/code]

but you can't do this:
[code]
void* ptrData = 0;
const short Data = 25;

ptrData = (void *)Data; // <-- requires the &, this produces big error :>
[/code]

sorry if i'm very vauge, just in a hurry to get answered :)
December 28, 2003, 8:30 PM
Skywing
Array names are treated as pointers to a constant memory location.
December 28, 2003, 8:48 PM
Yoni
[quote author=TheMinistered link=board=30;threadid=4472;start=0#msg37358 date=1072643445]
[code]
void* ptrData = 0;
const short Data = 25;

ptrData = (void *)Data; // <-- requires the &, this produces big error :>
[/code]
[/quote]
Error? You didn't specify whether you meant compiler error or runtime error.
This shouldn't cause a compiler error (maybe a warning though). After that line, ptrData should point to memory location "25".
About a runtime error, well, you didn't give any code following this code sample. If an access is made to memory location 25 then it will probably cause a page fault (I say probably because it isn't a strict rule and depends on the operating system/processor/environment/etc, but in most cases it does page fault).
December 28, 2003, 9:49 PM

Search