2015-03-31 70 views
0

我要分配從標準輸入一個struct值(即可以是最多50個字符)「的char [LENGTH]」,但正在錯誤:Ç - 不相容類型分配時輸入的內容從類型「字符*」

Incompatible types when assigning to type 'char[50]' from type 'char *'

#include <stdio.h> 
#include <stdlib.h> 

#define MAX_LEN 50 

struct msgbuf { 
char mtext[MAX_LEN]; 
}; 


int main (int argc, char *argv) 
{ 
    struct msgbuf m; 
    char in[MAX_LEN]; 

    scanf ("%s", in); 
    m.mtext = in; 

} 
+0

你爲什麼要將'in'強制轉換爲'char *'? – mstbaum 2015-03-31 19:00:16

+0

@mstbaum既然沒有什麼區別,我把它編輯出來 – BDillan 2015-03-31 19:01:44

+0

你不能指定一個數組到另一個。 – HuStmpHrrr 2015-03-31 19:02:25

回答

1

數組沒有複製賦值運算符。您必須逐個元素複製數組。您可以使用標頭<string.h>中聲明的標準功能strcpy來複制字符串。例如

#include <string.h> 

//... 

strcpy(m.mtext, in); 
相關問題