开发者

what does this pointer do?

char* p = "hello world";

programmers usually assigns the addres开发者_运维百科s of an variable to a pointer. But in the above case, where is the pointer pointing to?

and what in the world is

int x =42;
int* p= &x;
int ** r = &p;


It's pointing to an area in the program's read-only memory (usually in the program's machine code itself) containing the ASCII sequence hello world. Compare this to:

char p[] = "hello world";

Which creates an array of 12 chars on the stack, which is modifiable just like any other variable, and to:

char *p = strdup("hello world");

Which creates an array of 12 chars on the heap, and sets p to be a pointer to this readable and writable space of heap memory.

As for your (totally unrelated) second question:

int** r = &p;

Is simpler than it looks, though it's also bad. &p is the address of p. So if we did:

int x;
int *y = &x;

Then the pointer y points to the variable x, so assigning to *y changes x (and vice versa). We can do this for arbitrarily complex types:

int *x;
int **y = &x;

Now y is still a pointer that points to x, but x is also a pointer. So in your example, r is a pointer to a pointer to an int, and it's value is the address of p (a pointer to a char). However, it's a bad idea to do this because many platforms have alignment issues with casting from a char * type to a larger pointer type.


The string constant "hello world" must reside somewhere in the application and will be loaded into memory at runtime. Typically this is in the data section of the executable. The pointer p points to this address in memory.

The second example simply takes the address of p. Given that p is on the stack it will be an address into the current stack.


An illustration might be helpful. Given the following declarations:

char *s = "hello world";
int   x = 45;
int  *p = &x;
int **r = &p;
char q[] = "hello world";

assume the following memory map (addresses and layout are completely arbitrary and aren't meant to represent any real-world architecture):

                 0x00  0x01  0x02  0x03 
    0x00008000:  'h'   'e'   'l'   'l'   
    0x00008004:  'o'   ' '   'w'   'o'
    0x00008008:  'r'   'l'   'd'   0x00  
    ...
s:  0x01000000:  0x00  0x00  0x80  0x00  
x:  0x01000004:  0x00  0x00  0x00  0x2D
p:  0x01000008:  0x01  0x00  0x00  0x04
r:  0x0100000C:  0x01  0x00  0x00  0x08
q:  0x01000010:  'h'   'e'   'l'   'l'
    0x01000014:  'o'   ' '   'w'   'o'
    0x01000018:  'r'   'l'   'd'   0x00

The string literal "hello world" is a 12-element array of char (const char in C++) with static extent, meaning that memory is allocated for it when the program starts and remains allocated until the program terminates. Exactly where the string literal lives in memory depends on the platform, but it's best to assume that memory is unwritable (i.e., you can't change its contents with strcpy() or strcat() or sprintf(), etc.). The language standard explicitly states that attempting to modify a string literal results in undefined behavior.

The line

char *s = "hello world";

defines s as a pointer to char initializes it with the address of the literal (0x00008000 in this example).

The line

int x = 45;

defines x as an integer and initializes it with the value 45 (2D in hexadecimal notation).

The line

int *p = &x;

defines p as a pointer to int and initializes it with the address of x (0x01000004).

The line

int **r = &p;

defines r as a pointer to a pointer to int and initializes it with the address of p (0x01000008).

Note that pointer types are distinct and not always compatible. Even though s, p, and r all resolve to 32-bit address values in this particular hypothetical, they have different types and are not necessarily interchangable, even if they were all set to point to the same location. Some platforms use different sizes and representations for different pointer types.

And finally, as an added bonus, we have the line

char q[] = "hello world";

which defines q as a 12-element array of char (size taken from the size of the string literal being used to initialize it) and initializes it with the contents of the string literal.


The asterisk sign used when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator, also an asterisk.

If you want to access the value it returns, you must precede it with a double asterisk.


p points to the first character of a NUL-terminated string. r points to a pointer ... that is to say that it points to a location which holds pointer, to an integer.


p is a memory address stored with "hello world", so the memory p'address has a address position...

int** r = &p;

so r is a reference to that address...kinda messed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜