C: How do I change a variable's value by providing a memory address from scanf();
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.