LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-04-2002, 11:24 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
a question about obtaining address of variable .......


Hi. New to C and, linux <-- (sorta). In my C book (one of them anyways) I do the following example

#include <stdio.h>

int main(void)
{
char c;

printf("The address of c = 0x%p\n", &c);

return 0;
}

the problem is the ouput is

The address of c = 0x0xbfffa50



the example says to put the 0x in front of '%p' but it looks like it's printing another '0x'
and the output is supposed to be '0xbfffa50'

Im following the example except I think the author is using a windows machine
Am I doing something wrong here?


thanks
 
Old 07-05-2002, 02:43 AM   #2
Config
Member
 
Registered: Jan 2001
Location: Switzerland
Distribution: Gentoo
Posts: 376

Rep: Reputation: 30
Weird... if you output a pointer, the 0x gets added anyway. I have done this on Windows this way. And from what you're saying, Linux does this as well.
Except if... what happens if you ommit the & before the Variable c in the printf statement? I know this refers to the address of this variable, but may be, if you have a %p in the printf, it does that already.
Otherwise, you can send an E-Mail to the author
 
Old 07-05-2002, 10:32 AM   #3
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Rep: Reputation: 0
y r u using & before the variable name its used only when u have to accept the values into the variable.u can use this following code rather
main()
{
char c;
printf("the address of c =%u",c);
return 0;
}
 
Old 07-06-2002, 05:28 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You must pass '&c' to printf, not just 'c'. Remember, printf is not a builtin command - it has no way of obtaining the address of a variable that you pass it by value. Clearly the prepending of 0x is just a little formatting niceity provided by printf when it prints an address, it's not really a problem, is it?

Alex
 
  


Reply



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
Obtaining an IP Address Via Instant Messaging Services Permaximus Linux - Security 2 07-14-2005 09:30 AM
problems obtaining an wireless IP address ELconkestador Linux - Wireless Networking 2 01-23-2005 12:56 PM
Help obtaining ip address colombo187 Linux - Wireless Networking 4 12-28-2004 05:29 PM
obtaining a specific ip address from DHCP true_atlantis Slackware 5 09-28-2004 12:12 AM
Obtaining and IP address dynamicly getatmefosho Linux - Networking 3 12-10-2002 07:48 AM

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

All times are GMT -5. The time now is 06:32 PM.

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