Why does it not read the string matrix?

Asked

Viewed 153 times

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 just doesn’t read and it’s the first time I try dynamic matrix allocation. 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 ;

}
  • "malloc( 20*sizeof(char) ); /** cada nome possui no max 20 caracteres **/" for 20 characters in the name, you need to reserve space for 21 bytes. And also ... C/C++ does not exist. In C++ it is advised to use new, delete, etc; in C the use of malloc(), free(), etc is usual.

  • Yes. If it’s beautiful, but I’ve done a lot of c++ code using malloc() and free() and they’ve never been a problem.

  • @Alexandresantiagodasilva I went several times to SP in never suffered any accident until today.

  • 1

    @Alexandresantiagodasilva Did any of the answers solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.

2 answers

2

There may be more problems but the reading pattern is causing the problem. Is there any reason to use it? I took advantage and gave an organized in the code and fix 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]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I didn’t use the free() because it is a disposable example, in a real code I would have made the memory release. I also did not care about overflow of buffer, in real code this is a danger.

Theoretically this code of scanf() shall function to accept spaces. I couldn’t reproduce in ideone, may be a limitation of it:

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

But the OS question also has its syntax and does not work. I can not guarantee. If Nothing said to work, it is better to use the fgets(). Unfortunately I can not say because the reading pattern does not work, I can even give a search after.

I found a solution which I didn’t like much but solved. I would only like to be sure that she is right, which is not the case.

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

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

  • Very obg. But ( p + i ) is equivalent to p[i] and malloc(21sizeof(char)) equivalent to malloc(21) ( that in the case I put 20 ). But the malloc cast in your example ai.

  • I just put (char ** ) and (char* in the first and second allocation in your example and it worked. Thanks.

  • No, on the contrary, in your code is what’s left of these things. I took it consciously. Specifically the cast It’s better not to. It’s not just redundancy. http://stackoverflow.com/q/605845/221800 http://stackoverflow.com/q/1565496/221800 You may be using the C++ compiler. But this code is not C++. Choose what you will do, if you will program in C, use the C compiler, if you will use the C++ compiler, program in C++. The fact that the syntax is almost interchangeable does not mean that it should. Functioning is different from being right.

  • But it still has a problem. It is necessary to read the space by the scanf function

  • But without the cast gave problem at the time of debug. The problems were " invalid Conversion from void* to char**" and " invalid Conversion from void* to char*".

  • Good. I don’t have in-depth knowledge of compilers anymore, so I know, 'c' language functions can be used without problem in code in c++.

  • This is not quite true. Something can, not everything. And I will repeat, the fact that power does not mean that it should. Listen to what I am telling you: choose C or C++, do not mix the two styles. This will not get you to a good place. I improved the answer, I don’t know if it totally solves, I don’t have a better way to test, I can only try to see later what can be, but I gave alternatives.

  • Yes. I understood its point completely , I know that it should be taken into account in deeper development projects , but this is just a simple university job . What I’m trying to say is that using malloc() is not the cause of the code problem. But thanks for the help.

  • If you find something then send it here doing the favor.

Show 4 more comments

0

The instruction

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

want to read a string followed by a s. Like the s is part of the string, the scan never ends. Take away the s

scanf("%[^\n]", *(nomeEmpresas + i) );
//          ^^ sem s
  • Yes, it seems that the problem lies in the scanf function identifier. If I use scanf("%s", *(nameEmpresas + i) ); the program reads normally, but the same does not occur when I use scanf("%[ n]s", *(nameEmpresas + i) ); or even scanf("%[ n]", *(nameEmpresas + i) ); as I was told

  • The problem is that I need to read with spaces and use "%[ n]s" would be an option for me. But I don’t understand the problem.

Browser other questions tagged

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