C: How do I change a variable's value by providing a memory address from scanf();
Hello, I want to change a variable's value by providing a memory address. Like in Commondore 64:
Code:
HOPE <MEMORY-ADDRESS>, <VALUE> NOTE: I don't need the "split" thing to split and make it like a command prompt. Just ask for a variable's memory location. Please explain simply. :) |
well, when you define a variable
Code:
int var; Code:
int *address = &var; thus when you are doing this Code:
address++; and when you are doing this: Code:
(*address)++ so if you do something like Code:
void *pointer; you can also do something like this if you feel more safe: Code:
size_t p; Code:
int *po = p; |
I hope you remember: it was POKE in BASIC.
|
Cast?
|
A cast is a "change of type"
Code:
int a = 65; Obviously it doesn't always make sense |
Quote:
If long is shorter than pointer, you will end up with garbage in pointer. Quote:
That improves nothing. I don't use scanf enough myself to have any confidence of the right answer. |
johnsfine is right, there is not enough info to give a correct answer.
|
Note: in printf/scanf %zd/%zu might mean a pointer-sized integer (size_t, actually, but that should be compatible with uintptr_t)
The real question: how would the user know the address? Or they just enter random numbers and program interprets them as memory addresses? |
Quote:
Now I know: If you are using a C99 compatible version of scanf, you can use %zu to read a decimal number directly into a pointer variable. I think you can rely on size_t being the same size as a pointer, even when you can't rely on long being the same size as a pointer. I would have guessed the OP wanted to read the input in hex rather than in decimal, which I assume would be %zx, but the OP did not specify that (ask a better question in order to get a better answer). Quote:
|
I have not reviewed the C99 standard fully. I guess that is the answer then.
|
All times are GMT -5. The time now is 02:42 AM. |