Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-12-2012, 06:00 AM
|
#1
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Rep:
|
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. 
|
|
|
|
09-12-2012, 06:15 AM
|
#2
|
|
Member
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 257
Rep:
|
well, when you define a variable
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
you are moving to another memory space
and when you are doing this:
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
Last edited by Celyr; 09-12-2012 at 06:22 AM.
|
|
|
|
09-12-2012, 06:24 AM
|
#3
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,039
|
I hope you remember: it was POKE in BASIC.
|
|
|
|
09-12-2012, 07:02 AM
|
#4
|
|
Member
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 190
Original Poster
Rep:
|
Cast?
|
|
|
|
09-12-2012, 07:20 AM
|
#5
|
|
Member
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 257
Rep:
|
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
|
|
|
|
09-12-2012, 07:58 AM
|
#6
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,674
|
Quote:
Originally Posted by Celyr
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.
|
|
|
|
09-12-2012, 08:17 AM
|
#7
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,612
|
johnsfine is right, there is not enough info to give a correct answer.
|
|
|
|
09-12-2012, 08:37 AM
|
#8
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,039
|
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?
|
|
|
1 members found this post helpful.
|
09-12-2012, 09:35 AM
|
#9
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,674
|
Quote:
Originally Posted by H_TeXMeX_H
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".
Last edited by johnsfine; 09-12-2012 at 09:41 AM.
|
|
|
2 members found this post helpful.
|
09-12-2012, 11:56 AM
|
#10
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,612
|
I have not reviewed the C99 standard fully. I guess that is the answer then.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:05 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|