私は今Cで文字列について学んでいます。
scanf を使用して文字列を取得する方法
scanf("%s",str1);
printf の場合は、次のことができます
printf("The string is %s\n", str1);
scanf の場合は、文字列がポインタである単なる文字配列であるためだと理解していますが、printf の場合、int や float の場合と同じように変数名を入れることができるのはどうしてですか?
scanf
読み込む変数のアドレスが必要であり、文字列バッファーは既にアドレス (メモリ内の場所へのポインター、またはポインターに分解される配列) として表されています。
printf
同じことを行い%s
、文字列へのポインタとして扱います。
In C, variables that are arrays become a pointer to the first element of the array when used as function arguments -- so your scanf() sees a pointer to memory (assuming "str1" is an array).
In your printf(), "str1" could be either a pointer to a string or a character array (in which case the argument seen by printf() would be a pointer to the first element of the array).