LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   the address of an address does exist!!! (https://www.linuxquestions.org/questions/linux-newbie-8/the-address-of-an-address-does-exist-4175469662/)

v3ct0r 07-15-2013 04:04 AM

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?

eSelix 07-15-2013 05:28 AM

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.

v3ct0r 07-15-2013 06:36 AM

Quote:

Originally Posted by eSelix (Post 4990500)
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?

Doc CPU 07-15-2013 07:39 AM

Hi there,

Quote:

Originally Posted by xeechou (Post 4990528)
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 (Post 4990528)
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

v3ct0r 07-15-2013 08:21 AM

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.

jpollard 07-16-2013 09:59 AM

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...

rtmistler 07-16-2013 10:57 AM

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.


All times are GMT -5. The time now is 09:44 AM.