このコードを検討してください。
int main()
{
char *s, *t;
s = malloc(4 * sizeof(char));
strcpy(s, "foo");
t = s;
printf("%s %s\n", s, t); // Output --> foo foo
strcpy(s, "bar"); // s = "bar"
printf("%s %s\n", s, t); // Output --> bar bar
}
との 2 つの文字列がsありtます。最初に に設定sし"foo"、次に をt指摘しsます。文字列を印刷すると、foo foo.
次に、にコピー"bar"しsて再度印刷すると、bar bar.
tこの場合、値が変化するのはなぜですか? (なぜ変更したのかをコピー"bar"しました)。st
今、私がに変わるstrcpy(s, "bar")ときs = "bar"-
int main()
{
char *s, *t;
s = malloc(4 * sizeof(char));
strcpy(s, "foo");
t = s;
printf("%s %s\n", s, t); // Output --> foo foo
s = "bar"
printf("%s %s\n", s, t); // Output --> bar foo
}
このコードは私foo fooに とbar foo.
この場合、なぜ変更されなかったのですか?