[英]Pointer initialization and two pointers pointing to the same address
我試圖更好地理解指針。 我有這段代碼:
char theString[MAX] = "All your base are belong to us";
char *i = theString;
char *j = theString;
printf("%p\n", &theString);
printf("%p\n", &i);
printf("%p\n", &j);
當我運行程序時,我總是得到三個單獨的地址。 我知道每次編譯時內存地址都會改變,並且不是靜態的。 但是,由於我將i和j設置為“ theString”中第一個字符的地址,因此我假設它們將是相同的地址嗎?
非常感謝任何幫助,謝謝。
當我運行程序時,我總是得到三個單獨的地址。
您打印了三種不同內容的地址 : i
, j
和theString
。 如果打印i
和j
的值 ,您會發現它們都指向theString
。 &
運算符采用變量i
和j
本身的地址 。
這是修改后的打印語句,以兩種方式顯示變量:
printf("With &:%p\tWithout &:%p\n", &theString, theString);
printf("With &:%p\tWithout &:%p\n", &i, i);
printf("With &:%p\tWithout &:%p\n", &j, j);
輸出為:
With &:0x7ffeefbfee20 Without &:0x7ffeefbfee20
With &:0x7ffeefbfee08 Without &:0x7ffeefbfee20
With &:0x7ffeefbfee00 Without &:0x7ffeefbfee20
這表明您有三個不同的指針變量,它們都指向同一內存位置。
&j
和&i
是指針的地址,而不是它們引用的對象
printf("%p\n", (void *)i);
printf("%p\n", (void *)&j);
與int a = 3;
相同int a = 3;
並且int b = 3;
您不會期望&a
與&b
相同
嘗試這個:
printf("Address of theString = %p, value of theString = %s\n", (void *) theString, theString);
printf("Address of i = %p, value of i = %p\n", (void *) &i, (void *) i );
printf("Address of j = %p, value of j = %p\n", (void *) &j, (void *) j );
theString
, i
和j
都是內存中單獨的對象,因此它們都將具有不同的地址。 i
和j
將具有相同的值 ,即theString
的地址。
一些注意事項:
除非它是sizeof
或一元&
運算符的操作數,或者是用於在聲明中初始化字符數組的字符串文字,否則類型“ T
N元素數組”的表達式將被轉換(“ decay”)為類型為“指向T
指針”的表達式,該表達式的值將是第一個元素的地址。 因此,在第一個printf
語句中,不需要&
運算符即可獲取theString
的地址。 表達式theString
和&theString
都將求值為數組第一個元素的地址,但是表達式的類型將不同( char *
與char (*)[MAX]
)。
%p
轉換說明符期望其對應的參數的類型為void *
。 這是C語言中需要將指針表達式顯式轉換為void *
。
i
和j
的類型為char *
,因此表達式 &i
和&j
的類型為char **
。
編輯
我接受了您的聲明,並通過我自己的內存轉儲實用程序運行了它們:
#include "dumper.h"
#define MAX 32
int main( void )
{
char theString[MAX+1] = "All your base are belong to us";
char *i = theString;
char *j = theString;
char *names[] = {"theString", "i", "j" };
void *addrs[] = {theString, &i, &j};
size_t sizes[] = { sizeof theString, sizeof i, sizeof j };
dumper( names, addrs, sizes, 3, stdout );
return 0;
}
這是輸出:
Item Address 00 01 02 03
---- ------- -- -- -- --
theString 0x7ffee727fa90 41 6c 6c 20 All.
0x7ffee727fa94 79 6f 75 72 your
0x7ffee727fa98 20 62 61 73 .bas
0x7ffee727fa9c 65 20 61 72 e.ar
0x7ffee727faa0 65 20 62 65 e.be
0x7ffee727faa4 6c 6f 6e 67 long
0x7ffee727faa8 20 74 6f 20 .to.
0x7ffee727faac 75 73 00 00 us..
0x7ffee727fab0 00 00 00 00 ....
i 0x7ffee727fa20 90 fa 27 e7 ..'.
0x7ffee727fa24 fe 7f 00 00 ....
j 0x7ffee727fa18 90 fa 27 e7 ..'.
0x7ffee727fa1c fe 7f 00 00 ....
x86是低位字節序,因此需要從右到左,從下到上讀取多字節值。 您將看到i
和j
包含相同的值 ( 0x7ffee727fa90
),該值是theString
的第一個元素的地址。
i and j are char pointers , nothing but variables in short and like every variable they have their own locations and hence, their own addresses.
These variables hold the address of theString and hence both point to the same address
When you print &i and &j , it prints out the address of i and j in memory and hence, you get different addresses. But they hold the address of the same variable
Your code prints out the address of the array theString , pointer i address and pointer j address.
char theString[MAX] = "All your base are belong to us";
char *i = theString;
char *j = theString;
printf("%p\n", &theString);
printf("%p\n", &i);
printf("%p\n", &j);
關於:
printf("%p\n", &theString);
在C語言中,引用數組名稱會降級為數組第一個字節的地址,因此無需使用“ address of”運算符。 編譯器將抱怨:
: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘char (*)[##]’ [-Wformat=]
其中“ ##”是數組中元素的數量
請注意,i和j指向擁有自己內存的theString。 這就是為什么它們具有不同的價值。 但是指向字符數組的i,j和theString的值將相同。
printf("%p\n", theString);
printf("%p\n", i);
printf("%p\n", j);
輸出:
0x8efeefbfee50
0x8efeefbfee50
0x8efeefbfee50
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.