LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-12-2012, 06:00 AM   #1
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Rep: Reputation: 23
Question 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.
 
Old 09-12-2012, 06:15 AM   #2
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 81
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;

Last edited by Celyr; 09-12-2012 at 06:22 AM.
 
Old 09-12-2012, 06:24 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
I hope you remember: it was POKE in BASIC.
 
Old 09-12-2012, 07:02 AM   #4
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192

Original Poster
Blog Entries: 2

Rep: Reputation: 23
Cast?
 
Old 09-12-2012, 07:20 AM   #5
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 81
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
 
Old 09-12-2012, 07:58 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Celyr View Post
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.
 
Old 09-12-2012, 08:17 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
johnsfine is right, there is not enough info to give a correct answer.
 
Old 09-12-2012, 08:37 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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.
Old 09-12-2012, 09:35 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by H_TeXMeX_H View Post
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 View Post
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.
Old 09-12-2012, 11:56 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I have not reviewed the C99 standard fully. I guess that is the answer then.
 
  


Reply

Tags
common, gcc, linux, unix, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shared memory address area in process address space nagaraju1024 Programming 3 02-22-2011 04:49 AM
Application Virtual address space memory allocation - memory does not get free chamara82 Linux - General 4 01-01-2011 08:19 PM
[SOLVED] C scanf arbitrarily deletes contents of another variable??? phenyloxime Programming 13 10-06-2009 12:55 PM
dynamic memory allocation - malloc, scanf! cpd05 Programming 6 12-28-2006 06:11 PM
Can we specify variable field width in a scanf() format string? skie_knite007 Programming 3 05-13-2005 12:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration