文字列とポインタについて
Posted: 2017年12月24日(日) 15:34
以前、文字列の分割に関して質問させていただいた者です。
<URL:http://dixq.net/forum/viewtopic.php?f=3&t=19779>
前回教えていただいた際は気づかなったのですが、新たに2点わからない点がでてきたため、再度質問させていただければと思います。
読みづらく恐縮ですが、質問はコード内★箇所です。
念のため先に、質問箇所を抜粋します。
★「printf("%s\n", word); 」がエラーになるのはなぜか?
以下は手持ちの書籍のサンプルコートですが、ポインタによる文字列であるptrはちゃんと
「printf("ptr = \"%s\"\n", ptr);」
でも出力できているため、疑問に思いました。
******************************
int main(void)
{
char *ptr = "123";
printf("ptr = \"%s\"\n", ptr);
}
******************************
★「result = word[0];」とすると、
「warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘int’ [-Wformat=]」が出てしまう。
printf("%d\n", result)で試すと、分割した文字列要素の先頭文字のアドレスが入っていることは確認できるが、
printf("%S\n", result)として文字を直接出力できないのはなぜか?resultはchar型なので、そもそもintは入りようがないのではないか?
★ 「puts(result);」ではなく、「printf(result)」にできないのはなぜか?
====================
#include <stdio.h>
#include <string.h>
#define SIZE 64
int main(void)
{
int num = 0;
int i;
char string[SIZE]; //入力した文字列
char *word[SIZE]; //単語の位置が入る文字列。ポインタ配列なので直接文字代入可能。
char result[SIZE]; //最終的に出力する文字列
printf("文字列を入力してください\n");
string[0] = '\0'; //初期化
fgets(string, sizeof(string), stdin); //stringが入る
//strtokの使い方に注意
word[0] = strtok(string, " ");
while(word[num] != NULL)
{
word[++num] = strtok(NULL, " ");
}
printf("%s\n", word); //★エラーになるのはなぜか?
printf("%s\n", word[0]); //OK
printf("%s\n", *(word+1)); //OK
for (i = 0; i < num; i++)
{
result = word[0];
/*★ 「warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘int’ [-Wformat=]」が出る。
printf("%d\n", result)で試すとアドレスが入っていることは確認できるが、
printf("%S\n", result)として文字を直接出力できないのはなぜか? resultはchar型ではないのか? */
}
result[num] = '\0';
puts(result); /*★ printf(result)にできないのはなぜか
return 0;
}
お手数ですが何卒よろしくお願いします。
<URL:http://dixq.net/forum/viewtopic.php?f=3&t=19779>
前回教えていただいた際は気づかなったのですが、新たに2点わからない点がでてきたため、再度質問させていただければと思います。
読みづらく恐縮ですが、質問はコード内★箇所です。
念のため先に、質問箇所を抜粋します。
★「printf("%s\n", word); 」がエラーになるのはなぜか?
以下は手持ちの書籍のサンプルコートですが、ポインタによる文字列であるptrはちゃんと
「printf("ptr = \"%s\"\n", ptr);」
でも出力できているため、疑問に思いました。
******************************
int main(void)
{
char *ptr = "123";
printf("ptr = \"%s\"\n", ptr);
}
******************************
★「result = word[0];」とすると、
「warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘int’ [-Wformat=]」が出てしまう。
printf("%d\n", result)で試すと、分割した文字列要素の先頭文字のアドレスが入っていることは確認できるが、
printf("%S\n", result)として文字を直接出力できないのはなぜか?resultはchar型なので、そもそもintは入りようがないのではないか?
★ 「puts(result);」ではなく、「printf(result)」にできないのはなぜか?
====================
#include <stdio.h>
#include <string.h>
#define SIZE 64
int main(void)
{
int num = 0;
int i;
char string[SIZE]; //入力した文字列
char *word[SIZE]; //単語の位置が入る文字列。ポインタ配列なので直接文字代入可能。
char result[SIZE]; //最終的に出力する文字列
printf("文字列を入力してください\n");
string[0] = '\0'; //初期化
fgets(string, sizeof(string), stdin); //stringが入る
//strtokの使い方に注意
word[0] = strtok(string, " ");
while(word[num] != NULL)
{
word[++num] = strtok(NULL, " ");
}
printf("%s\n", word); //★エラーになるのはなぜか?
printf("%s\n", word[0]); //OK
printf("%s\n", *(word+1)); //OK
for (i = 0; i < num; i++)
{
result = word[0];
/*★ 「warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 2 has type ‘int’ [-Wformat=]」が出る。
printf("%d\n", result)で試すとアドレスが入っていることは確認できるが、
printf("%S\n", result)として文字を直接出力できないのはなぜか? resultはchar型ではないのか? */
}
result[num] = '\0';
puts(result); /*★ printf(result)にできないのはなぜか
return 0;
}
お手数ですが何卒よろしくお願いします。