2017-03-03 271 views
0

我想在C程序設計語言 串的矩陣,這是我的代碼字符串矩陣輸入輸出

void main() 
{ 
    char Data[10][3][20]; 
    int i=0; 
    int j=0; 
    for (i=0;i<10;i++) 
    { 
     for (j=0;j<3;j++) 
     { 
      Data[i][j]="aa"; 
     } 
    } 
    for (i=0;i<10;i++) 
    { 
     for (j=0;j<3;j++) 
     { 

      printf("%s",Data[i][j]); 
     } 
    } 
    printf("Done"); 
    scanf("%d",&i); 
} 

時遇到的錯誤是:assignment to expression with array type 請給我解釋一下我做錯了,因爲這是我試圖在我的原始代碼中使用的原型是製作「用戶名,密碼,級別」的數據庫

謝謝您的進一步信息。

+0

你做了什麼?請提供你的問題[mcve]。 –

+1

'數據[i] [j]'是一個*數組*。你不能分配給數組,只能複製到它。閱讀['strcpy'](http://en.cppreference.com/w/c/string/byte/strcpy)。 –

+0

爲了將來的參考,*請* [閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –

回答

1

Data[i][j]是一個數組。您不能分配給數組,只能複製到它。使用strcpy()。在http://www.cplusplus.com/reference/cstring/strcpy/

#include <stdio.h>  
int main() { 
    char Data[10][3][20]; 
    int i=0; 
    int j=0; 
    for (i=0;i<10;i++){ 
     for (j=0;j<3;j++){ 
      strcpy(Data[i][j], "aa"); //use strcpy for copy values 
     } 
    } 
    for (i=0;i<10;i++){ 
     for (j=0;j<3;j++) {  
      printf("%s ",Data[i][j]); 
     } 
     printf("\n"); 
    } 
    printf("Done"); 
    scanf("%d",&i); //why this scanf here ?? 
    return 0; 
} 
+0

非常感謝 –

+0

@AmirBouker如果這解決了你的錯誤,不要忘記+1。 :) – roottraveller

0

更多的細節你正在創建的char數組,你不能分配(指針)到它。這就是爲什麼你得到錯誤assignment to expression with array type

雖然可以將字符串複製到數組元素。嘗試使用strcpy而不是代碼中的以下作業:

Data[i][j]="aa";