• Welcome to Valhalla Legends Archive.
 

Simple C++ Question

Started by TheMinistered, December 28, 2003, 02:30 PM

Previous topic - Next topic

TheMinistered

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

Skywing

Array names are treated as pointers to a constant memory location.

Yoni

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