Copy Strings in C

3

I have the following two-dimensional array of strings. char matriz[linhas][tamanhoDaString] Through strcpy I copied a string there.

char *aux = "abc";
strcpy(matriz, aux);

My goal was to set linha 3 to aux .

How can I solve my problem?

    
asked by anonymous 23.03.2014 / 16:15

2 answers

2

Just put the index, so you can specify the position of your 2D array you want to copy the value to:

strcpy(matriz[2], aux);
    
23.03.2014 / 16:26
6

matriz is not a string: it is an array of strings. You can not use matriz directly in str* functions; you have to use your elements.

The third element of matriz is matriz[2] , this element is (or rather, can be ) a string.

const char *aux = "abc"; // eu gosto de por o const para o compilador me
                         // avisar se eu tentar alterar o conteúdo de aux
if (strlen(aux) >= sizeof *matriz) {
    // o tamanho de cada elemento de matriz
    // nao é suficiente para o comprimento de aux
} else {
    strcpy(matriz[2], aux);
}
    
23.03.2014 / 16:27