gpt4 book ai didi

C - 程序崩溃执行

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

我的程序在显示“Quel est ce mot ?”后崩溃了:
我怎么能找到,我怎么能解决这个问题?
期待您的评论:)
感谢您的帮助!

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wjean-ma <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/08/03 03:07:53 by wjean-ma #+# #+# */
/* Updated: 2015/08/08 21:43:49 by wjean-ma ### ########.fr */
/* */
/* ************************************************************************** */

#include "include/libft.h"

char *ft_putword(char **client, char *str)
{
int i;

i = 0;
while (str[i])
{
if ((*client)[i] != str[i])
(*client)[i] = '*';
else
(*client)[i] = str[i];
i++;
}
return (*client);
}

int main(void)
{
char *to_find;
char *client;
int size;
int buffer;

to_find = "Violet";
size = ft_strlen(to_find) + 1;
if ((client = (char *)malloc(sizeof(char) * (size))) == NULL)
return (-1);

client = "******";
while (ft_strcmp(client, to_find) != 0)
{
ft_putstr("Quel est ce mot: ");
ft_putstr(ft_putword(&client, to_find));
ft_putstr("\n> ");
scanf("%s", client);
while ((buffer = getchar()) != '\n')
;
ft_putstr(ft_putword(&client, to_find));
ft_putchar('\n');
}
ft_putstr("Done\n");
free(client);
return (0);
}

/* ------------------------------------------ ------------------ */

最佳答案

此语句:client = "******";client到字符串文字的地址,编译器将其放入只读存储器中。然后,当您尝试使用 scanf("%s", client); 更改此内存时它会导致崩溃。

您可以将客户端初始化为数组,如:char client[] = "******";所以编译器会把它放在一个可写的内存段中。

关于C - 程序崩溃执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897905/

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