2015-08-08 43 views
1

显示“Quel est ce mot?”后程序崩溃:
我该怎么办,我该如何解决这个问题?
期待您的意见:)
感谢您的帮助!C - Prog crash exec

/* ************************************************************************** */ 
/*                   */ 
/*              :::  :::::::: */ 
/* 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); 
} 

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

+2

如何找到:使用调试器! – Amit

+0

使用'gdb'调试器 –

+1

这将试图修改RO内存:(调试器?绝对! –

回答

0

此声明:client = "******";client设置为字符串文字的地址,编译器将其置入其中只有记忆。然后当你试图用scanf("%s", client);改变这个内存时,它会导致崩溃。

您可以将客户端初始化为如下所示的数组:char client[] = "******";,以便编译器将其放入可写入的内存段中。