LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2013, 05:04 AM   #1
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Rep: Reputation: Disabled
the address of an address does exist!!!


Code:
#include <stdio.h>
int atoi(char *ps);
int main() {
	char *s = "hello world";
	printf("The address of string s is 0x%x\n", s);
	
	printf("The address of address of string s is 0x%x\n", &s);
        atoi(&ps);
}
Usually we just wanna know the address of string or array, but this time I do need operate the address. transfer it to the sub function, that is because the program is not that easy, you need to trust me.

the funny thing is the address of address does exist. Does it just happen by chance?
 
Old 07-15-2013, 06:28 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 315Reputation: 315Reputation: 315Reputation: 315
Yes, address of address exists. If you store something in memory, like defined in main() variable "s" (which self is an address, but this is irrelevant), its address is available. As always remember that this variable will be destroyed at end of function, so make sure you will not access it beyond scope.
 
Old 07-15-2013, 07:36 AM   #3
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by eSelix View Post
Yes, address of address exists. If you store something in memory, like defined in main() variable "s" (which self is an address, but this is irrelevant), its address is available. As always remember that this variable will be destroyed at end of function, so make sure you will not access it beyond scope.
Yeah, then I tried to change &p(p is a pionter), which is not possible. I tried the code following:
Code:
#include <stdio.h>

int main() {
	char *s = "hello world";
	printf("The address of string s is 0x%x\n", s);
	int i;
	for (i = 0; i <= 20; i++) {
	printf("The address of address of string s is 0x%x\t and stores 0x%x\n", &s, s++);
	printf("%c\n", *s);}

}
then I got this:
The address of string s is 0x400628
The address of address of string s is 0xf43c5a90 and stores 0x400628
e
The address of address of string s is 0xf43c5a90 and stores 0x400629
l
The address of address of string s is 0xf43c5a90 and stores 0x40062a
l
The address of address of string s is 0xf43c5a90 and stores 0x40062b
o
The address of address of string s is 0xf43c5a90 and stores 0x40062c

The address of address of string s is 0xf43c5a90 and stores 0x40062d
w
The address of address of string s is 0xf43c5a90 and stores 0x40062e
o
The address of address of string s is 0xf43c5a90 and stores 0x40062f
r
The address of address of string s is 0xf43c5a90 and stores 0x400630
l
The address of address of string s is 0xf43c5a90 and stores 0x400631
d
The address of address of string s is 0xf43c5a90 and stores 0x400632

The address of address of string s is 0xf43c5a90 and stores 0x400633

The address of address of string s is 0xf43c5a90 and stores 0x400634

The address of address of string s is 0xf43c5a90 and stores 0x400635

The address of address of string s is 0xf43c5a90 and stores 0x400636

The address of address of string s is 0xf43c5a90 and stores 0x400637
T
The address of address of string s is 0xf43c5a90 and stores 0x400638
h
The address of address of string s is 0xf43c5a90 and stores 0x400639
e
The address of address of string s is 0xf43c5a90 and stores 0x40063a

The address of address of string s is 0xf43c5a90 and stores 0x40063b
a
The address of address of string s is 0xf43c5a90 and stores 0x40063c
d

look ,the address of the string's address doesnt change anyway. What's going on?
 
Old 07-15-2013, 08:39 AM   #4
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 343Reputation: 343Reputation: 343Reputation: 343
Hi there,

Quote:
Originally Posted by xeechou View Post
Yeah, then I tried to change &p(p is a pionter), which is not possible.
yes, it is. Let's assume your definition:

Code:
char *s = "hello world";
So p points to the beginning of the string - to the 'h' character, to be precise. Consequently, printf("%s", p) will print the entire string, that is, the character that p points to, and all following characters up to the end of the string. Likewise, printf("%c", p) will print only the single character p is currently pointing at.

You could even change the characters that p points to. For example:

Code:
char *s = "hello world";
*s = 'b';   // change the character that p points at
printf("%s", s);
That will print the string "bello world", because the 'h' at the beginning has been overwritten by a 'b'.

Code:
#include <stdio.h>

int main() {
	char *s = "hello world";
	printf("The address of string s is 0x%x\n", s);
	int i;
	for (i = 0; i <= 20; i++) {
	printf("The address of address of string s is 0x%x\t and stores 0x%x\n", &s, s++);
	printf("%c\n", *s);}

}
From the output of this, you notice a few things:
  • The address where s itself is stored remains unchanged.
  • The contents of s, which is the memory location it points to, increases by one at each loop iteration.
  • As a consequence, each time the loop is repeated, the next character in the string is printed, as s "walks" through the string.
However, this becomes interesting when s walks past the end of the string:

Code:
The address of address of string s is 0xf43c5a90         and stores 0x400630
l
The address of address of string s is 0xf43c5a90         and stores 0x400631
d
The address of address of string s is 0xf43c5a90         and stores 0x400632

The address of address of string s is 0xf43c5a90         and stores 0x400633

The address of address of string s is 0xf43c5a90         and stores 0x400634

The address of address of string s is 0xf43c5a90         and stores 0x400635

The address of address of string s is 0xf43c5a90         and stores 0x400636

The address of address of string s is 0xf43c5a90         and stores 0x400637
T
The address of address of string s is 0xf43c5a90         and stores 0x400638
h
After the 'd' of "hello world", there are five lines that seem to print nothing. Actually, the first of these five must be a NUL character, which doesn't produce a printable output, but marks the end of a string in C. I'm not sure about the next four, but probably they represent the int i that is declared next. Since the value of i is between 12 and 15 then, it's stored as three null bytes, and one counting from 12 to 15. Neither of these codes produce a visible output at the console.
Then, finally, you recognize the beginning of the next string in your program, "The address of ...". That's how your variables are stored in memory.

Quote:
Originally Posted by xeechou View Post
look ,the address of the string's address doesnt change anyway. What's going on?
The location of a signpost doesn't change, even if you write different things on the sign.
The number of a hotel room doesn't change when guests check out and in.

Like more analogies? - You really should get a grip on basics.

[X] Doc CPU
 
Old 07-15-2013, 09:21 AM   #5
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Thanks very much, DOC, I've been attaching my eyez on the where the pointer pionts to and if there is a pionter piont to it.
I just forget p is a variable, so OF CAUSE IT DOES HAS A ADDRESS.

Quote:
You really should get a grip on basics.
Well, that's what I'm working on, but damn it, I wasted a whole day on this pionter thing.
 
Old 07-16-2013, 10:59 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
Your problem is that the example you chose is more complex than you think.

Pointer variable s, and integer i are both on the stack. The string pointed to by s is on the heap.

All are derived from additional pointers (s and i from the stack frame, which is pointed to by the stack pointer). The heap has its own pointer (usually a fixed base - fixed as in it is fixed for a specific program, it varies depending on the size of the program).

Walking through a string (incrementing a pointer) is fine --- but the technique allows you to point to nonsense after you reach the end of the defined string...
 
Old 07-16-2013, 11:57 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,968
Blog Entries: 12

Rep: Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036Reputation: 2036
printf() has %p to give you the value in a pointer, hence you could've created a pointer to the string and used printf() to get you the result.

Code:
char *s = "hello world";
printf("The address of string s is %p\n", s);
And by the way, %p at least under Linux will give you the 0x already included.
 
  


Reply


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
external: domain of sender address does not exist talking to my server eagleone Linux - Networking 1 11-02-2005 09:54 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 05:33 AM
Sendmail: Domain of sender address does not exist 360 Linux - Networking 1 02-02-2003 09:40 PM
Sendmail Domain of sender address does not exist mantiev Linux - Networking 0 05-08-2001 08:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration