Ricsie
Ricsie

Reputation: 319

Copy Array of strings in C error

I want to save about 20 country names into one string and then copy them to another but it always do me some mistake, can anybody help me with it?

This is my code:

char array1[30][30], array2[30][30];

This is how i put them into first array:

fscanf(fr, "%s", array1[i]);

This all works but when i want to do:

array2[0] = array1[0];

I get error:

incompatible types when assigning to type 'char[30]' from type 'char *'

When I use:

strcpy(array2[pom2], array1[i]);

it shows no error but doesn't copy it nor print it out.

Upvotes: 2

Views: 2454

Answers (2)

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18042

Did you try passing character by character?

for( i = 0; i < 30; i++ ){
   for( j = 0; j < 30; j++ ){
      targetArray[ i ][ j ] = sourceArray[ i ][ j ];

      /* End of the string, stop copying */
      if ( sourceArray[ i ][ j ] == '\0' ){
         break;
      }
   }
}

Upvotes: 1

Fabien
Fabien

Reputation: 13446

For the first error : you cannot copy an array into another one. You can just copy salar values (chars, in your case).

If you want to copy a string into another, you indeed have to use the strcpy function (or a close relative, as strncpy. You should give us the full code so that we can see where the problem is with your call to strcpy.

Upvotes: 1

Related Questions