简体   繁体   English

打印存储指针指向数组的地址 C

[英]Print the address that stores the pointer points to an array in C

This question may seems quite duplicated;这个问题可能看起来很重复; however, I indeed have had a serious survey on this site and still cannot quite understand.但是,我确实在这个网站上进行了认真的调查,但仍然不太了解。

    char str[] = "test";
    printf("%p\n", str);
    printf("%p\n", &str);

I know str itself is a pointer which points to the starting location that stores "t".我知道str本身是一个指向存储“t”的起始位置的指针。 Therefore I expect that printf("%p\n", str);因此我希望printf("%p\n", str); shows this address (say 000000FFE94FFC34 ).显示此地址(比如000000FFE94FFC34 )。 Next, I wish to know in my OS, at what memory location that stores the exact information 000000FFE94FFC34 .接下来,我想知道在我的操作系统中,在哪个 memory 位置存储了确切的信息000000FFE94FFC34 (ie the address of str itself.) (即str本身的地址。)

However, the output of the third line is the same as the previous one.但是第三行的output和上一行是一样的。 It seems quite a weird behavior.这似乎是一种很奇怪的行为。 And how can I figure out the address of str itself?我怎样才能找出str本身的地址? I guess this can be achieved by using another new pointer that points to the pointer I want, ie char **super_pointer = str;我想这可以通过使用另一个指向我想要的指针的新指针来实现,即char **super_pointer = str; but I think it is an unnecessarily complicated way.但我认为这是一种不必要的复杂方式。

Lets draw it out with the pointers added, to hopefully make it simpler to understand:让我们用添加的指针将其绘制出来,希望能使其更易于理解:

+--------+--------+--------+--------+--------+
| str[0] | str[1] | str[2] | str[3] | str[4] |
+--------+--------+--------+--------+--------+
^
|
&str[0]
|
&str

As you can see the pointer to the first element ( &str[0] which is what plain str decays to) points to the same location as the array itself ( &str ).如您所见,指向第一个元素的指针( &str[0]是普通str衰减到的位置)指向与数组本身 ( &str ) 相同的位置。

But (and it's an important but): The type are very different!但是(这是一个重要的但是):类型非常不同!

The type of &str[0] is char * . &str[0]的类型是char *

The type of &str is char (*)[5] . &str的类型是char (*)[5]


On another note you say that在另一张纸条上你说

... str itself is a pointer... ... str本身是一个指针...

This is wrong .这是错误的。 str itself is the array, which can decay to a pointer to its first element. str本身是数组,它可以衰减到指向其第一个元素的指针。

I know str itself is a pointer which points to the starting location that stores "t".我知道 str 本身是一个指向存储“t”的起始位置的指针。

As you wrote "str itself" is not a pointer it is an array.正如您所写的那样,“str 本身”不是一个指针,而是一个数组。 But used in expressions array designators with rare exceptions are converted to pointers to their first elements.但是在表达式中使用的数组指示符在极少数情况下被转换为指向其第一个元素的指针。

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)来自 C 标准(6.3.2.1 左值、arrays 和 function 指示符)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 3 除非它是 sizeof 运算符或一元 & 运算符的操作数,或者是用于初始化数组的字符串文字,否则类型为“类型数组”的表达式将转换为类型为“指针”的表达式to type'' 指向数组 object 的初始元素并且不是左值。 If the array object has register storage class, the behavior is undefined.如果数组 object 具有寄存器存储 class,则行为未定义。

In this call在这次通话中

printf("%p\n", str);

the array designator str used as an argument expression is converted to a pointer to its first element.用作参数表达式的数组指示符str被转换为指向其第一个元素的指针。 In fact this call is equivalent to实际上这个调用等同于

printf("%p\n", &str[0]);

That is this call outputs the starting address of the extent of memory occupied by the array.即本次调用输出数组占用的memory范围的起始地址。

This call这个电话

printf("%p\n", &str);

that is better to write as and the previous call like最好写成和之前的调用一样

printf("%p\n", ( void * )&str);

also outputs the starting address of the extent of the memory occupied by the array.同时输出数组占用的memory范围的起始地址。

What is the difference?有什么区别?

The expression str used in the call of printf has the type char * while the expression &str has the type char ( * )[5] . printf调用中使用的表达式str的类型为char *而表达式&str的类型为char ( * )[5]

The both expressions is interpreted by the call as expressions of the type void * .这两个表达式都被调用解释为void *类型的表达式。

To make this more clear consider a two dimensional array为了更清楚地考虑一个二维数组

char str[2][6] = { "test1", "test2" };

The addresses of the array as whole and of its first "row" and of its first character of the first "row" are coincide.整个数组的地址和它的第一个“行”的地址以及第一个“行”的第一个字符的地址是一致的。

That is the expression &sgtr that has the type char ( * )[2][6] and the expression &str[0] that has the type char ( * )[6] and the expression &str[0][0] that has the type char * yield the same value: hhe starting address of the extent of memory occupied by the array..这是具有类型char ( * )[2][6]的表达式&sgtr和具有类型 char ( * char ( * )[6] [6] 的表达式&str[0]以及具有类型的表达式&str[0][0]类型char *产生相同的值: hhe 数组占用的范围 memory 的起始地址..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM