Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: TheMinistered on December 28, 2003, 02:30 PM

Title: Simple C++ Question
Post by: TheMinistered on December 28, 2003, 02:30 PM
Let me show you a few code samples that retain to the question first:

void* ptrBuffer = 0;
const char DataBuffer[] = "Hello World!";


how come you can do this without the & (adress of) operator:

ptrBuffer = (void*)DataBuffer;


but you can't do this:

void* ptrData = 0;
const short Data = 25;

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


sorry if i'm very vauge, just in a hurry to get answered :)
Title: Re:Simple C++ Question
Post by: Skywing on December 28, 2003, 02:48 PM
Array names are treated as pointers to a constant memory location.
Title: Re:Simple C++ Question
Post by: Yoni on December 28, 2003, 03:49 PM
Quote from: TheMinistered on December 28, 2003, 02:30 PM

void* ptrData = 0;
const short Data = 25;

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

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).