How to Store Strings in Vectors - C

Asked

Viewed 4,471 times

3

How do I store more than one name in a type variable char, why put, let’s assume name[20], I’m defining that she was given 20 characters, and not that she could receive 20 elements, the code I’m trying to test this, is this one.

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


int i;
float tvendas[10], pvendas[10];
char nome[15];

int main(void){

    setlocale(LC_ALL, "Portuguese");

    for(i=0; i<=9; i++)
    {

        printf("%d° VENDEDOR | DIGITE O TOTAL DE VENDAS: R$ ", i+1);
        scanf("%f", &tvendas[i]);

        printf("%d° VENDEDOR | DIGITE O PERCENTUAL DE AUMENTO DAS VENDAS: ", i+1);
        scanf("%f", &pvendas[i]);

        printf("%d° VENDEDOR | DIGITE O NOME: ", i+1);
        scanf("%s", nome[i]);  // MEU PROBLEMA É AQUI

    }

    for(i=0; i<=9; i++){
    printf("TESTE %s\n", nome[i]); //TESTANDO PARA VER SE OS NOMES APARECEM.
    }

return 0;
}
  • wouldn’t be, for example: char nome[15][15];

  • That’s right, thank you very much, I didn’t know you could do that.

  • Blza, I’ll put in an answer to facilitate ;)

2 answers

4

To show another alternative, and in some ways as a complement to the @aa_sp response, you can also use an array of pointers to char. In this scenario it is necessary to allocate each name individually in the for before using.

//...
char *nome[15]; //vetor de ponteiros para char (strings), 15 nomes

int main(void){
    //...
    for(i=0; i<=9; i++){
        //...
        nome[i] = malloc(sizeof(char) * 30); //aloca espaço uma string de 29 carateres
        scanf("%s", nome[i]); //scanf igual ao que tinha
    }

    for(i=0; i<=9; i++){
        printf("TESTE %s\n", nome[i]); //printf também igual ao que tinha
    }
    //...

For the example you have, the solution to allocate in stack as the @aa_sp answer showed is preferable as it is faster, simpler and avoids having to free up memory when you no longer need the strings.

This shape I showed is useful in at least two scenarios:

  1. When you create strings in a function and need them to exist beyond the function’s lifespan
  2. If you want the space reserved for each name to be different.

As a final note I used sizeof(char) just to make it clear that it’s the size of char which is supporting the malloc even if it is guaranteed to always result in 1.

  • I think this would be the "C way" to do, using pointer array, because in "real life" (outside of Stackoverflow) I’ve never seen a two-dimensional array used in C...

  • Thank you very much, I have to study on these pointers too, using the form of aa_sp worked on quite a few exercises, but in some, is joining one name with the other let’s assume, Paul and Victor when I put a printf to show the names gets Pavitor, I have to find a way to fix this.

3


You can make a two-dimensional array:

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


int i;
float tvendas[10], pvendas[10];
char nome[15][15];

int main(void){

    setlocale(LC_ALL, "Portuguese");

    for(i=0; i<=9; i++)
    {

        printf("%d° VENDEDOR | DIGITE O TOTAL DE VENDAS: R$ ", i+1);
        scanf("%f", &tvendas[i]);

        printf("%d° VENDEDOR | DIGITE O PERCENTUAL DE AUMENTO DAS VENDAS: ", i+1);
        scanf("%f", &pvendas[i]);

        printf("%d° VENDEDOR | DIGITE O NOME: ", i+1);
        scanf("%s", nome[i]);  // MEU PROBLEMA É AQUI

    }

    for(i=0; i<=9; i++){
    printf("TESTE %s\n", nome[i]); //TESTANDO PARA VER SE OS NOMES APARECEM.
    }

return 0;
}

Browser other questions tagged

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