#1
by Bulls » 2年前
参考問題を書きかえて、str_copy関数の呼び出しをprintf関数の実引数とするプログラムを作成せよ。
<参考問題>
#include <stdio.h>
char *str_copy(char *d, const char *s)
{
char *t = d;
while (*d++ = *s++);
return t;
}
int main(void)
{
char str[128] = "ABC";
char tmp[128];
printf("str = \"%s\"\n", str);
printf("コピーするのは : ", tmp);
scanf("%s", tmp);
str_copy(str, tmp);
puts("コピーしました。");
printf("str = \"%s\"\n", str);
return 0;
}
<私の回答>
#include <stdio.h>
char *str_copy(char *d, const char *s)
{
char *t = d;
while (*d++ = *s++);
return t;
}
int main(void)
{
char str[128] = "ABC";
char tmp[128];
printf("str = \"%s\"\n", str);
printf("コピーするのは : ");
scanf("%s", tmp);
puts("コピーしました。");
printf("str = \"%s\"\n", str_copy(str, tmp));
return 0;
}
<参考問題>
str = "ABC"
コピーするのは : 4
コピーしました。
str = "4"
私の回答をコンパイルしましたが、以下のようにエラーになってしまいます。指摘された点をそのまま直すとコピー後も"ABC"と表示されます。
どこが間違っているのか何が足りないのか教えていただけますでしょうか。
<エラー内容>
enshu11-3.c:7:14: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while (*d++ = *s++);
~~~~~^~~~~~
enshu11-3.c:7:14: note: place parentheses around the assignment to silence this warning
while (*d++ = *s++);
^
( )
enshu11-3.c:7:14: note: use '==' to turn this assignment into an equality comparison
while (*d++ = *s++);
^
==
enshu11-3.c:23:27: error: cannot take the address of an rvalue of type 'char *'
printf("str = \"%s\"\n", &str_copy(str, tmp));
よろしくお願いいたします。
参考問題を書きかえて、str_copy関数の呼び出しをprintf関数の実引数とするプログラムを作成せよ。
<参考問題>
#include <stdio.h>
char *str_copy(char *d, const char *s)
{
char *t = d;
while (*d++ = *s++);
return t;
}
int main(void)
{
char str[128] = "ABC";
char tmp[128];
printf("str = \"%s\"\n", str);
printf("コピーするのは : ", tmp);
scanf("%s", tmp);
str_copy(str, tmp);
puts("コピーしました。");
printf("str = \"%s\"\n", str);
return 0;
}
<私の回答>
#include <stdio.h>
char *str_copy(char *d, const char *s)
{
char *t = d;
while (*d++ = *s++);
return t;
}
int main(void)
{
char str[128] = "ABC";
char tmp[128];
printf("str = \"%s\"\n", str);
printf("コピーするのは : ");
scanf("%s", tmp);
puts("コピーしました。");
printf("str = \"%s\"\n", str_copy(str, tmp));
return 0;
}
<参考問題>
str = "ABC"
コピーするのは : 4
コピーしました。
str = "4"
私の回答をコンパイルしましたが、以下のようにエラーになってしまいます。指摘された点をそのまま直すとコピー後も"ABC"と表示されます。
どこが間違っているのか何が足りないのか教えていただけますでしょうか。
<エラー内容>
enshu11-3.c:7:14: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
while (*d++ = *s++);
~~~~~^~~~~~
enshu11-3.c:7:14: note: place parentheses around the assignment to silence this warning
while (*d++ = *s++);
^
( )
enshu11-3.c:7:14: note: use '==' to turn this assignment into an equality comparison
while (*d++ = *s++);
^
==
enshu11-3.c:23:27: error: cannot take the address of an rvalue of type 'char *'
printf("str = \"%s\"\n", &str_copy(str, tmp));
よろしくお願いいたします。