C Array of string
        Posted  
        
            by Meko
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Meko
        
        
        
        Published on 2010-05-22T10:17:52Z
        Indexed on 
            2010/05/22
            10:20 UTC
        
        
        Read the original article
        Hit count: 249
        
HI. This is maybe simple question but I want to create two dimensional array and add it string like in java
string  str = "text" ; 
string [][] array  = new [][] string ;  
array[i][j] = str ; 
But in C there is no string .I tried like this but here strcpy() gives error.It returns to assembly code. I am trying to read line by line from text and split line by space and add them to structure.But first I think that I must add each line and row in array and then making iteration and adding to structures fields.
    static const char filename[] = "student.txt";
FILE *file = fopen ( filename, "r" );
char line [ 128 ]; /* or other suitable maximum line size */
char delims [ ]=" ";
char *result =NULL;
char list[15];
char arra[128][128];
int i=0;
int j=0;
struct {
    char gruppa[10];
    char familiya[20];
    int uchaste;
    struct {
        int firsth;
        int second;
        int third;
        int fourht;
        int fifth;
    } exam;
}student;
for(i=0; i<128; i++)
    for(j=0; j<128; j++) 
        arra[i][j] = '\0';
for(i=0; i<15; i++)
    list[i] = '\0';
if ( file != NULL )
{           
    while ( fgets ( line, sizeof line, file ) != NULL ) 
    {
        result = strtok(line,delims);
                    while (result !=NULL) {
            strcpy(list,("%s",result));
                strcpy(arra[i][j],list); // Here it gives errror 
            j++;
            result = strtok(NULL,delims);
        }
        j=0;
        i++;
    }
    fclose ( file );
}
else
{
    perror ( filename ); 
}
getchar();
return 0;
}
© Stack Overflow or respective owner