Hi again,
Is there a way to input directly into the memory and then access the input through a pointer?
Hi again,
Is there a way to input directly into the memory and then access the input through a pointer?
It depends on what you mean by directly into memory, this works:
But if you mean directly to a specific memory address then I would say if you don't know how to do it, you aren't supposed to:Code:main(){ int x; int *p = &x; *p = 10; }
Code:main(){ unsigned int *p = (unsigned int *)0x12345678; }
i agree, rarely will you ever need to point at absolute memory addresses, and when you need to, you'll know how to do it. if you would like the program to set aside some memory for you and then write to that, use malloc ().
hello, internet!
So, just what exactly do you _think_ is happening when you access a variable?!?!Is there a way to input directly into the memory and then access the input through a pointer?
The array 'name[]' is at a specific address in RAM. Stuffing a letter into each position of the array means the address of the array and it's offset must be obtained and then data copied into that location.Code:char name[5]; int main(void) { name[0] = 'W'; /* access address via pointer */ name[1] = 'O'; name[2] = 'W'; name[3] = 0x00; return(0); };
It is not the spoon that bends, it is you who bends around the spoon.
>> The array 'name[]' is at a specific address in RAM
It seems to me people used the word "fixed." name[] isn't always in the same place whereas 0x12345678 is. At the machine level accessing memory locations is okay. In other words, unless you are making a video card driver or something to that effect you won't be writing anything like int *i = (int *)0xA1FFFF000
The original requestor didn't ask for the same address in RAM, they asked, instead, how to put a value directly into RAM.It seems to me people used the word "fixed." name[] isn't always in the same place whereas 0x12345678 is.
That was the question I answered.
If you want to stuff a value directly into a never-changing address (ignoring seg faults and other gotchas), then:
And even that doesn't put it in the same physical location-- the value 0x004E37a9 (or any other) is relative to the start address of your heap zone. Unless you're working in the O/S Heap.Code:unsigned char *theAddressP; theAddressP = 0x004E37A9; /* arbitrary address */ theAddressP[0] = 'a'; /* stuff an a there */
*************
WARNING
*************
The above code corrupts memory. It is for educational purposes only.
Last edited by Sayeh; 12-10-2002 at 08:25 AM.
It is not the spoon that bends, it is you who bends around the spoon.