2012-11-07 61 views
1
#include<stdio.h> 
int main() 
{ 
    int *p=0; 
    char *ch=0; 
    p++; 
    ch++; 
    printf ("%d and %d\n",p,ch); 
    return 0; 
} 

輸出印刷:爲什麼地址差是越來越(根據指針型)

4 and 1 
  1. 我知道在它指向太地址炭指針遞增爲1。

  2. 我知道指向int的指針在gcc的地址中增加爲+4。

  3. 我知道Derefrencing一個指針應該通過使用*指針來完成。

查詢:

  1. 這是爲什麼不給p和CH因爲二者都是指針,並沒有分配任何地址的垃圾值;

  2. 爲什麼這給了我的地址差異,即相應的指針已經獲得增量,或者是這是一個未定義的行爲

3. 爲什麼輸出4和1?

P1。說明。

我已經在gcc-4.3.4上編譯了這段代碼。 它是一個C代碼。 如果這是一個問題的副本,我很抱歉,因爲我無法在stackoverflow上找到任何此類問題。

回答

2

首先遞增,您的代碼打印指針爲整數。雖然這可能是您要做的事情,但這並不是定義的行爲,因爲在指針大小(以字節爲單位)不同於int的大小的平臺上它完全不可移植。如果要打印指針值,請改爲使用%p

要回答你的問題。你分配值指針:,這是NULL的同義詞。

二。你得到4 1的原因是由於你的平臺上的尺寸爲int,而尺寸爲char。字符將變爲1.在您的平臺上,int的寬度爲4個字節。當增加一個指針時,編譯器會自動將它引用的地址移動它所代表的底層類型的字節數。

#include<stdio.h> 
int main() 
{ 
    int *p=0; // int is 4 bytes on your platform 
    char *ch=0; // char is 1 byte 
    p++;   // increments the address in p by 4 
    ch++;  // increments the address in ch by 1 
    printf ("%d and %d\n",p,ch); 
    return 0; 
} 

編輯:你會得到類似的結果,但有一個支持的打印語句,而是執行此操作:

#include<stdio.h> 
int main() 
{ 
    int *p=0; 
    char *ch=0; 
    p++; 
    ch++; 
    printf ("%p and %p\n",p,ch); 
    return 0; 
} 

輸出(在我的Mac)是:

0x4 and 0x1 
+0

所以它不是一個未定義的行爲。它會根據指針大小,int和char在不同的機器上給出相同的輸出。 –

+2

@tapasweni你需要再讀一遍。 **使用帶有「%d」格式說明符和指針參數的'printf()'是未定義的行爲。**增加指針,甚至NULL指針肯定是***行爲。 – WhozCraig

+0

這意味着指針p和ch中的地址現在是4和1,前面是零,這是因爲遵循地址在我的機器中的存儲規則。如果我錯了,我會糾正我。 –

0

按我的知識,我已經加入了回答您的問題在線:

#include<stdio.h> 
int main() 
{ 
    int x=0,*p=0; 
    char c = 'A', *ch=0; 
    x++; 

    // You have initialized this to 0, so incrementing adds 4 (int pointer) 
    // Remember, the address '4' means nothing here 
    p++; 

    // You have initialized this to 0, so incrementing adds 1 (char pointer) 
    // Remember, the address '1' means nothing here 
    ch++; 

    // You are now printing the values of the pointers itself 
    // This does not make any sense. If you are using pointers, you would want to know what is being referenced 
    printf ("%d , %d and %d\n",x,p,ch); 

    // This will FAIL 
    // Because, you are now trying to print the data pointed by the pointers 
    // Note the use of '*'. This is called de-referencing 
    printf ("%d , %d and %d\n", x, *p, *ch); 

    // Let p point to x, de-referencing will now work 
    p = &x; 
    printf ("%d , %d\n", x, *p); // 1, 1 

    // Let ch point to c, de-referencing will now work 
    ch = &c; 
    printf ("%c , %c\n", c, *ch); // 'A', 'A' 

    return 0; 
} 

希望這有助於。

+0

Pl。解釋爲什麼要打印4 1.我知道我並沒有對指針表示拒絕。你能告訴p =&x; 012fprintf(「%d,%d \ n」,x,* p); // 4,4爲什麼4,4 –

+1

由於0 + 4 = 4和0 + 1 = 1,並且由於Type *的值遞增1,所以指針引用的字節地址由sizeof(Type) '(類型的大小爲1單位,所以在增量之後,它指向類型的下一個值)。 –

+1

我不認爲它會打印4,4它會打印1,1.as x = 1後增量和p的地址x和* p將是1. Pl。糾正我,如果我錯了。 –

3

1.爲什麼這不給p和ch的垃圾值,因爲它們都是指針並且沒有分配任何地址;

err,您在這裏指定的地址>int *p = 0char *ch = 0p包含地址0x00000000ch包含地址0x00000000

爲什麼說這個給我,同時增加相應的指針獲得的地址不同,或者這是一個不確定的 行爲。

char *ch = 0;意味着ch包含地址0。使用++遞增地址將使該值增加sizeof(char) viz 1。類似的整數。 p包含地址0。並且使用++運算符將您的計算機上的值增加(似乎是4)(注意,並非總是如此,特別是對於64位計算機)。

3.爲什麼這個輸出是4 1?這裏

由於在第一,p包含0,則通過sizeof(type_of(p)) = sizeof(int) = 4在機器上遞增,並且通過ch = sizeof(type_of(ch)) = sizeof(char) 1.