gpt4 book ai didi

c - 使用 gets() 在指针中输入字符串

转载 作者:行者123 更新时间:2023-11-30 17:08:18 25 4
gpt4 key购买 nike

#include <stdio.h>

void main()
{
char *p;
gets(p);
puts(p);
}

当我运行代码时会发生这种情况 enter image description here

最佳答案

您必须声明该指针需要多少内存。例如,当您输入: *p="Hello world"时,它会自动声明为此使用了多少内存。因此,如果您仍然想在该程序中使用指针,那么您必须使用 malloc 函数:

#include<stdio.h>

int main()
{
char* p;
p=malloc(100);

printf("Enter some text:");
gets(p);
printf("\nYou have typed: %s\n",p);

return 0;
}

这里是malloc(100);意味着你需要 100 字节内存来存储该字符串。

关于c - 使用 gets() 在指针中输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33723889/

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