ページ 1 / 1
ポインタあ
Posted: 2015年2月05日(木) 21:32
by ポインタ
#include <stdio.h>
int main() {
char *str = "YUKI \0 RENA \0 MIMI";
printf("%s\n%s\n%s" , str , str + 7 , str + 14);
return 0;
}
これを 実行すると
YUKI
RENA
MIMI
となるのはなぜでしょうか。
なぜstr+7でRENAがでて
なぜstr+14でMIMIが出るのか教えてください。
str+7だとAだと思うのですが。
Re: ポインタあ
Posted: 2015年2月05日(木) 21:42
by みけCAT
コードはBBcodeを有効にした状態でcodeタグで囲み、かつ適切にインデントをしていただけると、見やすくてありがたいです。
(現状のポインタさんのインデントは適切です)
ポインタ さんが書きました:なぜstr+7でRENAがでて
なぜstr+14でMIMIが出るのか教えてください。
str + 7は、strが指すchar型の要素の7個次の要素を指すポインタになります。
数えてみると、RENAの先頭のRを指していることがわかります。
コード:
0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18
--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
Y |U |K |I | |\0| |R |E |N |A | |\0| |M |I |M |I |\0
printfのフォーマット指定%sは、長さの指定が無いので、指定されたポインタからNUL文字に当たるまで連続して文字を書き出します。
The C89 Draft - 4.9.6.1 The fprintf function さんが書きました:
The conversion specifiers and their meanings are
(中略)
s The argument shall be a pointer to an array of character type. Characters from the array are written up to (but not including) a terminating null character; if the precision is specified, no more than that many characters are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
(
http://port70.net/~nsz/c/c89/c89-draft.html#4.9.6.1)
よって、printfの%sフォーマットにstr + 7を渡すと、この場合"RENA "が出力されます。
MIMIについても同様に考えればいいです。
ポインタ さんが書きました:str+7だとAだと思うのですが。
逆に、どうしてそう考えたのか教えていただけますか?
Re: ポインタあ
Posted: 2015年2月05日(木) 21:46
by box
みけCAT さんが書きました:
ポインタ さんが書きました:str+7だとAだと思うのですが。
逆に、どうしてそう考えたのか教えていただけますか?
' ' や '\0' を無視して考えるとそうなりそうです。推測ですけど。
この推測が当たっているとして、' ' や '\0' を無視してはいけないことは言うまでもありません。
Re: ポインタあ
Posted: 2015年2月05日(木) 22:06
by ポインタ
str+7がAだと思ったのはアルフェベットだけをカウントしていたからでした。
空文字(空白)と \0 の違いってなんですか?
Re: ポインタあ
Posted: 2015年2月05日(木) 22:15
by みけCAT
ポインタ さんが書きました:空文字(空白)と \0 の違いってなんですか?
空白は空白文字であり、文字コードで表すと半角ならASCIIで0x20、全角ならShift_JISで0x81 0x40などです。
\0はC言語ではエスケープシーケンスであり、NUL文字(ASCIIコード0x00)と解釈されます。
コード:
#include <stdio.h>
int main(void) {
printf("%d %d\n", ' ', '\0');
return 0;
}
Re: ポインタあ
Posted: 2015年2月05日(木) 22:18
by ポインタ
みなさまありがとうございます。
Re: ポインタあ
Posted: 2015年2月05日(木) 22:24
by ポインタ
#include <stdio.h>
int main(void) {
printf("%d %d\n", ' ', '\0');
return 0;
}
これを実行した結果
32 0となるのですが、どこから32がでてきたんでしょうか。
また0はNULLという解釈で大丈夫ですか?
Re: ポインタあ
Posted: 2015年2月05日(木) 22:32
by みけCAT
ポインタ さんが書きました:32 0となるのですが、どこから32がでてきたんでしょうか。
32は半角空白のASCII文字コードです。
ポインタ さんが書きました:また0はNULLという解釈で大丈夫ですか?
いいえ、0 (整数)とNULL (C言語では通常「どこも指してない」ポインタ)は別物です。
The C89 Draft さんが書きました:An integral constant expression with the value 0, or such an expression cast to type void * , is called a null pointer constant. If a null pointer constant is assigned to or compared for equality to a pointer, the constant is converted to a pointer of that type. Such a pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
(
http://port70.net/~nsz/c/c89/c89-draft.html#3.2.2.3)
The C89 Draft さんが書きました: The macros are
NULL
which expands to an implementation-defined null pointer constant
(
http://port70.net/~nsz/c/c89/c89-draft.html#4.1.5)