ポインタあ

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ポインタ

ポインタあ

#1

投稿記事 by ポインタ » 10年前

#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だと思うのですが。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: ポインタあ

#2

投稿記事 by みけCAT » 10年前

コードは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だと思うのですが。
逆に、どうしてそう考えたのか教えていただけますか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

box
記事: 2002
登録日時: 14年前

Re: ポインタあ

#3

投稿記事 by box » 10年前

みけCAT さんが書きました:
ポインタ さんが書きました:str+7だとAだと思うのですが。
逆に、どうしてそう考えたのか教えていただけますか?
' ' や '\0' を無視して考えるとそうなりそうです。推測ですけど。
この推測が当たっているとして、' ' や '\0' を無視してはいけないことは言うまでもありません。
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

ポインタ

Re: ポインタあ

#4

投稿記事 by ポインタ » 10年前

str+7がAだと思ったのはアルフェベットだけをカウントしていたからでした。
空文字(空白)と \0 の違いってなんですか?

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: ポインタあ

#5

投稿記事 by みけCAT » 10年前

ポインタ さんが書きました:空文字(空白)と \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;
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

ポインタ

Re: ポインタあ

#6

投稿記事 by ポインタ » 10年前

みなさまありがとうございます。

ポインタ

Re: ポインタあ

#7

投稿記事 by ポインタ » 10年前

#include <stdio.h>

int main(void) {
printf("%d %d\n", ' ', '\0');
return 0;
}

これを実行した結果

32 0となるのですが、どこから32がでてきたんでしょうか。
また0はNULLという解釈で大丈夫ですか?

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: ポインタあ

#8

投稿記事 by みけCAT » 10年前

ポインタ さんが書きました: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)
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

“C言語何でも質問掲示板” へ戻る