Matrix of strings

6

How do I declare and start an array of strings ?

I tried in many ways and I still did not succeed, I thought it would work fine but it did not work.

#include <stdio.h>
#include <stdlib.h>

int main(void){
    char matriz[2][2];
    char string1[] = "Minha string";
    matriz[0][0] = string1;
    printf("%s", matriz[0][0]);
    return 0;
}
    
asked by anonymous 05.09.2017 / 14:38

1 answer

5

Always consider that a string is a type char * (pointer to characters), even if you use a char * it's still a pointer] 1 (could use an array there in certain circumstances, so if you want an array of strings should use type char * as the type of array.

#include <stdio.h>

int main(void){
    char *matriz[2][2];
    char string1[] = "Minha string";
    matriz[0][0] = string1;
    printf("%s", matriz[0][0]);
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
05.09.2017 / 14:47