2012-03-08 80 views
0
char input[INPUT_SIZE]; /*Holding user input globaly*/ 

char history[50][INPUT_SIZE]; /*Storing last 0 commands*/ 

void addToHistory() 
{ 
    history[0] = input; 
    printf("#: %s \n", history[0]); 
} 

fgets(input,INPUT_SIZE,stdin) /*Using this to get the input*/ 

IM的與fgets保存輸入,然後我希望能夠調用添加到歷史記錄功能,以當前輸入保存了歷史的第一個值,但我不斷收到錯誤messsgage ...C字符串數組Manipulaion

「錯誤:不兼容的類型分配給輸入時 '的char [512]' 從類型「字符*」 伊夫tryed使用&輸入,*輸入,但其仍然是相同..

我似乎不能解決這個問題..

回答

3

您不能分配到一個數組,你應該複製到:

memcpy(history[0], input, sizeof(history[0])); 
+1

或用['的strcpy()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strcpy.html) ,它可以避免複製未使用的字節:) – pmg 2012-03-08 11:29:38

+1

@pmg - 你是對的,我錯過了一些問題的部分,只是看到了作業:) – MByD 2012-03-08 11:33:14