gpt4 book ai didi

c - 使用带有指向字符的指针的 scanf 函数

转载 作者:太空狗 更新时间:2023-10-29 16:45:59 25 4
gpt4 key购买 nike

我写了下面一段代码:

int main() {
char arrays[12];
char *pointers;
scanf("%s", arrays);
scanf("%s", pointers);
printf("%s", arrays);
printf("%s", pointers);
return 0;
}

为什么写scanf("%s", pointers)会报错?

最佳答案

char *pointers;

必须初始化。在将字符串指向某个地址之前,您不能将字符串扫描到 pointers 中。计算机需要知道在哪里存储它从键盘读取的值。

int main() {
char arrays[12];
char *pointers = arrays;
scanf("%s", pointers);
printf("%s", pointers);
return 0;
}

关于c - 使用带有指向字符的指针的 scanf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14546018/

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