按我的知識,我已經加入了回答您的問題在線:
#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;
}
希望這有助於。
所以它不是一個未定義的行爲。它會根據指針大小,int和char在不同的機器上給出相同的輸出。 –
@tapasweni你需要再讀一遍。 **使用帶有「%d」格式說明符和指針參數的'printf()'是未定義的行爲。**增加指針,甚至NULL指針肯定是***行爲。 – WhozCraig
這意味着指針p和ch中的地址現在是4和1,前面是零,這是因爲遵循地址在我的機器中的存儲規則。如果我錯了,我會糾正我。 –