Reputation: 27
My program crash after displayed "Quel est ce mot ?" :
How I can foud, how I can resolve this ?
Looking forwards your comments : )
Thank's for your help !
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wjean-ma <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
/* ----------------------------------------------------------------- */
Upvotes: 1
Views: 105
Reputation: 856
This statement: client = "******";
sets client
to the address of the string literal, which the compiler puts in read only memory. Then when you try to change this memory with scanf("%s", client);
it will cause a crash.
You could initialize the client as an array like: char client[] = "******";
so the compiler will put it in a write-able memory segment.
Upvotes: 1