我試圖按照書中的一些步驟。這是書中的確切代碼,但我收到了一條錯誤消息。如何使用C和printf函數打印指針內部的內容?
這兩個printf語句的是問題:
printf(pointer);
printf(pointer2);
如何解決這個實際打印什麼是裏面的指針?
#include <stdio.h>
#include <string.h>
int main(void)
{
char str_a[20]; //A 20-element array
char *pointer; //A pointer, meant for a character array
char *pointer2; //And yet another one
strcpy(str_a, "Hello World\n");
pointer = str_a; //Set the first pointer to the start of the array
printf(pointer);
pointer2 = pointer + 2; //Set the second one 2 bytes further in.
printf(pointer2);
strcpy(pointer2, "y you guys\n"); //Copy into that spot.
printf(pointer);
return 0;
}
的printf( 「%S」,str_a)?但請注意,如果以這種方式使用printf是一種安全風險。讓舒爾str_a總是終止。 – EGOrecords
使用'printf(「%s」,pointer_variable);'或'fputs(pointer_variable,stdout);' – BLUEPIXY
謝謝你們兩位,這些答案是快速和準確的。 @BLUEPIXY,我用你的方法,它的工作。 – Mekanic