我是c編程的新生,我做了一個小程序,在動作中看到指針中++的所有組合。因此,我codded:(ⅰ通過的virtualbox在Ubuntu運行它)指針的地址逐漸增加
#include <stdio.h>
main()
{
int num = 1;
int *p = #
printf("\nnum position: %p num value: %d\n", p, num);
*p += 1;
printf("\n---- *p += 1; -----\n");
printf("num position: %p num value: %d\n", p, num);
*p++;
printf("\n---- *p++; -----\n");
printf("num position: %p num value: %d\n", p, num);
(*p)++;
printf("\n---- (*p)++; -----\n");
printf("num position: %p num value: %d\n", p, num);
++(*p);
printf("\n---- ++(*p); -----\n");
printf("num position: %p num value: %d\n", p, num);
++*p;
printf("\n---- ++*p; -----\n");
printf("num position: %p num value: %d\n\n", p, num);
}
輸出:
num position: 0xbfce07b8 num value: 1
---- *p += 1; -----
num position: 0xbfce07b8 num value: 2
---- *p++; -----
num position: 0xbfce07bc num value: 2
---- (*p)++; -----
num position: 0xbfce07bd num value: 2
---- ++(*p); -----
num position: 0xbfce08bd num value: 2
---- ++*p; -----
num position: 0xbfce08bd num value: 2
我明白++的不同用法。但是我的問題是關於地址(我只是寫了4個位數)
起初,地址是:07b8
的第一增量後的地址爲07bc第二增量後(增加4(HEX))
地址爲07bd(增加1(十六進制))
第3次增量後地址爲08bd(增加100(十六進制))
爲什麼地址增量不穩定?
不要忘記添加返回0;在主要結束時。總是使用return 0,並且嘗試使用int main()而不是main(),因爲c99嚴格檢查返回類型 – niko