LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C: How do I change a variable's value by providing a memory address from scanf(); (https://www.linuxquestions.org/questions/programming-9/c-how-do-i-change-a-variable%27s-value-by-providing-a-memory-address-from-scanf-%3B-4175426841/)

suttiwit 09-12-2012 06:00 AM

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>
How can I get the variable memory location from scanf(); which the user will input it and edit the variable's 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. :)

Celyr 09-12-2012 06:15 AM

well, when you define a variable
Code:

int var;
you can get it's address via
Code:

int *address = &var;
so address is the position in memory of the var
thus when you are doing this
Code:

address++;
you are moving to another memory space
and when you are doing this:
Code:

(*address)++
you are incrementing the value of var :)

so if you do something like
Code:

void *pointer;
scanf("%ld", &pointer);

you can access to that memory area by *pointer (well you have to cast it to a type before because you cant deference a void* type ofc)
you can also do something like this if you feel more safe:
Code:

size_t p;
scanf("%ld", &p);

then you have to cast it for example
Code:

int *po = p;

NevemTeve 09-12-2012 06:24 AM

I hope you remember: it was POKE in BASIC.

suttiwit 09-12-2012 07:02 AM

Cast?

Celyr 09-12-2012 07:20 AM

A cast is a "change of type"
Code:

int a = 65;
char b = (char)a;

This is a cast.
Obviously it doesn't always make sense

johnsfine 09-12-2012 07:58 AM

Quote:

Originally Posted by Celyr (Post 4778376)
Code:

void *pointer;
scanf("%ld", &pointer);


On an architecture where long is larger than pointer, you will corrupt the memory after &pointer on the stack.
If long is shorter than pointer, you will end up with garbage in pointer.

Quote:

you can also do something like this if you feel more safe:
Code:

size_t p;
scanf("%ld", &p);


So instead you assume long and size_t are the same size ?!
That improves nothing.

I don't use scanf enough myself to have any confidence of the right answer.

H_TeXMeX_H 09-12-2012 08:17 AM

johnsfine is right, there is not enough info to give a correct answer.

NevemTeve 09-12-2012 08:37 AM

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?

johnsfine 09-12-2012 09:35 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 4778458)
johnsfine is right, there is not enough info to give a correct answer.

No, actually johnsfine didn't do the right google search (and never uses C99) and didn't know the info NevemTeve just posted.

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:

Originally Posted by NevemTeve (Post 4778481)
The real question: how would the user know the address? Or they just enter random numbers and program interprets them as memory addresses?

I wondered about that as well. We get a lot of "how to" questions here, where you need to wonder whether the OP really ought to be doing the thing he asks how to do. It is often a close judgement call whether to answer "here is how" vs. "I think your plan to do that is based on a deeper misunderstanding".

H_TeXMeX_H 09-12-2012 11:56 AM

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.