How to print a string from a string array in C?

Asked

Viewed 16,507 times

0

I have a string vector(palavras[x][y]), I can read every word that will be an element of the vector, but I cannot print any of these stored words.

printf("%s",palavras[a][b]), doesn’t work.

Here are my attempts:

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

int main(int argc, char *argv[])
{
char nomes[1][15];
int i;
scanf("%s",&nomes[0][15]);
scanf("%s",&nomes[1][15]);
printf("%s",&nomes[0][15]);
// for(i=0;i<=15;i++){
// printf("%c",nomes[0][i]);
// }
system("PAUSE");    
return 0;
}

The for commented also did not work. How can I proceed?

  • You want a vector with only one string, which means he’ll only have one element with a string up to 14 characters? If it is more than one, is it always 2? You need to use a loop for some reason or it can be the simplest way?

  • he counts from 0 then would be 2 strings and 16 characters, words[0][15] = 'his' ; words[1][15]='comment'. I would like the simplest way to write any of these vectors on the screen.

  • I need a method for an n string array.

  • I need to print some of these strings.

  • Is there a reason you are trying to print string characters one by one using %c, instead of printing the whole string with %s ?

  • I left the string fixed, and the string characters would vary until the string is finished, in the for loop. But it also didn’t work.

  • but what I want is to print the whole string.

Show 2 more comments

1 answer

1


Problems

Let’s start by analyzing the problems in the code.

  • If he was trying to create a vector of 2 strings, as indicated in the comment, so the size did not go right as it was declared as char nomes[1][15]; and so it got size 1 instead of 2.

  • Both the scanfs as the printfs are not right. Let’s look at the first scanf:

    scanf("%s", &nomes[0][15]); //leitura incorreta
    

    He’s trying to read a string (%s) but then pass the address of the 16th characteristic of the first string, and that’s why it’s not right.

    Instead I should just do it like this:

    scanf("%s", nomes[0]); //ler um texto para a primeira posição do array de strings
    
  • In the for was also trying to print letter by letter but this is much more complicated than printing the string all with %s, just as it would also be necessary to stop if you found the terminator \0.

Solution

Setting the previously mentioned details your code would look like this:

int main(int argc, char *argv[])
{
    char nomes[2][15]; //agora com tamanho para 2 strings

    scanf("%s", nomes[0]);
    scanf("%s", nomes[1]); 
    printf("%s" ,nomes[0]);

    int i;
    for(i=0; i<2; i++) {
        printf("%s ",nomes[i]); //agora com %s
    }

    return 0;
}

See the example in Ideone

  • @Gustavoviana what comes inside [ ] indicates the size, so how many allows it. If you have nomes[1][...] so there’s only so much 1. nomes[2][15] means size 2, ie 2 strings and 15 characters each being that has to have room for the \0 soon will only have 14 useful careteres for each string

  • then I saw it now in an apostille, it counts from 0 but for vector[2], it will exist, vector[0], vector[1](2 vectors). I really believed he counted to 2. Thanks for the help. I got it, the characters would be 14 + 0, for vector[15].

  • @Gustavoviana Exatamente, vetor[2] will make it have 2 elements(strings) the 0 and the 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.