gpt4 book ai didi

c - 使用fgets存储字符指针程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:03 25 4
gpt4 key购买 nike

char *p;
fgets(p,10,stdin);

为什么这会导致我的程序崩溃?好像我不能在 Fgets 中使用 char 指针,但我可以使用数组,所以它没有意义。

最佳答案

Seems like I can't use char pointers in Fgets but I can use arrays

不,您可以同时使用两者,只要您在将指针传递给 fgets() 之前为指针分配内存即可。

在这种情况下,使用指针,

 char *p;

p 被初始化并指向某个无效的任意内存位置。您需要为 p 分配适当的内存,然后才能通过 p 读取/写入内存位置指针。

关于c - 使用fgets存储字符指针程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252318/

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