我編譯如下代碼:指針常量用C
#include <stdio.h>
int main(void) {
// your code goes here
char *consta = "ABC";
printf("Use of just const: %c\n", consta);
printf("Use of const[1]: %c\n", consta[1]);
printf("Use of whole string: %s", consta);
return 0;
}
但是,我得到的輸出是:
Use of just const: P
Use of const[1]: B
Use of whole string: ABC
第二printf和但預期第三printf函數調用工作,我在第一次調用printf時期待'A'被打印而不是'P'。
未定義行爲再次觸發。 – alk 2014-09-22 16:33:27
你需要這個:http://www.amazon.ca/C-Primer-Plus-5th-Edition/dp/0672326965你目前是否參加了一門關於C的課程,或者只是教給你自己? – DevNull 2014-09-22 16:35:17
* printf *函數不是類型安全的。在編譯器中啓用所有警告。 – 2014-09-22 16:35:18