0

I want to make a matrix of strings in C Programming language this is my code

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);
}

the error am having is : assignment to expression with array type please explain to me what am doing wrong because this is a prototype am trying to use in my original code that is to make a data base of "username,Password,level"

thank you inadvance.

mch
  • 9,424
  • 2
  • 28
  • 42

2 Answers2

1

Data[i][j] is an array. You can't assign to an array, only copy to it. use strcpy(). more details at 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;
}
roottraveller
  • 7,942
  • 7
  • 60
  • 65
0

You are creating an array of char and you cannot assign (a pointer) to it. This is why you are getting the error assignment to expression with array type.

You can copy the string to the array elements though. Try using strcpy instead of the below assignment in your code:

Data[i][j]="aa";
Jay
  • 24,173
  • 25
  • 93
  • 141