2015-08-08 45 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[] = "******";,以便編譯器將其放入可寫入的內存段中。