gpt4 book ai didi

c - 为什么 x[2] 也重新分配 y 值?我该如何解决这个问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:16:55 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <cs50.h>

int main(void)
{

string y, x;
y = x = get_string();

x[2] = '\0';

printf("%s", x);
printf("%s", y);

}

如果输入是abcdef。此代码的输出是 abab。为什么不是 ababcdef

最佳答案

那是因为 yx 指向 get_string 返回的同一个字符串。

get_string :

Reads a line of text from standard input and returns it as a string (char *), sans trailing newline character. [...]

关于c - 为什么 x[2] 也重新分配 y 值?我该如何解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43466635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com