Linux - NewbieThis 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
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.
#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?
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.
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?
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
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.
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.