Why does not the string array read?

0

I am creating a code that for now needs to read the name of N companies (whose N is given). I'm trying to enter the names but the program simply does not read and is the first time I try to allocate dynamic arrays. The code is here:

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

void LeiaDadosDasEmpresas( int quantEmpresas , char ** nomeEmpresas ){

    int i ;

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

        printf("\nDigite o nome da %dª empresa : ", i + 1 );

        scanf("%[^\n]s", *(nomeEmpresas + i) );

    }

}


int main()
{

    int quantEmpresas, i ;
    char ** nomeEmpresas ;

    printf("Digite o numero de empresas que queira analisar : ");
    scanf("%d", &quantEmpresas );

    nomeEmpresas = (char **) malloc( quantEmpresas*sizeof(char *) );

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

        *(nomeEmpresas + i) = (char *) malloc( 20*sizeof(char) ); /** cada nome possui no max 20 caracteres ( incluindo '/0 ' ) **/

    }

    LeiaDadosDasEmpresas( quantEmpresas , nomeEmpresas );

    return 0 ;

}
    
asked by anonymous 07.01.2016 / 17:00

2 answers

2

There may be some more problems but the reading pattern is causing the problem. Do you have any reason to use it? I took advantage of it and organized it in the code and fixed other small problems:

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

void LeiaDadosDasEmpresas(int quantEmpresas, char ** nomeEmpresas) {
    for (int i = 0; i < quantEmpresas; i++) {
        printf("\nDigite o nome da %dª empresa: ", i + 1);
        scanf("%s", nomeEmpresas[i]); // <=========================== mudei aqui
    }
}
int main() {
    int quantEmpresas;
    char ** nomeEmpresas;
    printf("Digite o numero de empresas que queira analisar: ");
    scanf("%d", &quantEmpresas);
    nomeEmpresas = malloc(quantEmpresas * sizeof(char *));
    for (int i = 0; i < quantEmpresas; i++) {
        nomeEmpresas[i] = malloc(21);
    }
    LeiaDadosDasEmpresas(quantEmpresas, nomeEmpresas);
    for (int i = 0; i < quantEmpresas; i++) {
        printf("\nNome da %dª empresa: %s", i + 1, nomeEmpresas[i]);
    }
    return 0;
}

See running on ideone .

I did not use free() because it is a disposable example, in real code I would have made the memory release. Also I did not mind with buffer overflow, in actual code this is a danger.

In theory, code of scanf should work to accept spaces . I could not reproduce in ideone, it could be a limitation of it:

scanf("%21[^\n]", nomeEmpresas[i]);

But in the question the OS also has its syntax and does not work. I can not guarantee that. If Nothing said to work, you'd better use fgets() . Unfortunately I can not say why the reading pattern does not work, I can even give it a lookup later.

I found a solution that I did not like much but solved. I would only like it if I was sure she was right, which is not the case.

scanf("%[^\n]%*c", nomeEmpresas[i]);

I still prefer the solution with fgets() which is more secure.

    
07.01.2016 / 17:21
0

The instruction

scanf("%[^\n]s", *(nomeEmpresas + i) );
//          ^^^ s a mais

you want to read a string followed by a s . Because s is part of the string, scan never ends. Take out the s

scanf("%[^\n]", *(nomeEmpresas + i) );
//          ^^ sem s
    
07.01.2016 / 17:25