How to extend the storage capacity of char variables

Asked

Viewed 846 times

2

hello! I am having problems with a "mini dictionary" that I am mounting.... It is basically ready, but the definition of words, because they are too long, present an error when they are displayed. I suppose it is because of some limitation of the type of variable used. If anyone can help me I would be grateful. Follows the code:

#include <stdio.h>
#include <stdbool.h>

struct dicionario {

    char palavra[21];
    char definicao[51];

};

bool compararpalavras (const char palavra1[], const char palavra2[]) {

    int x = 0;

    while (palavra1[x] == palavra2[x] && palavra1[x] != '\0' && palavra2[x] !='\0') {

        ++x;

}
    if (palavra1[x] == '\0' && palavra2[x] == '\0') {

        return true;

    } else {

        return false;

    }
}

int procurarpalavras (const struct dicionario lingua[], const char palavra[], const int numdepalavras) {


    bool compararpalavras (const char palavra1[], const char palavra2[]);

    int x = 0;

    while (x < numdepalavras) {

        if (compararpalavras( lingua[x].palavra, palavra)) {

            return x;

        } else {

             ++x;

        }       
    }
    return -1;  
}

int main (void) {

    int procurarpalavras (const struct dicionario lingua[], const char palavra[], const int numdepalavras);

    const int NUMERODEDEFINICOES = 7;
    char palavra[21] = {'\0'};
    int resultadopesquisa;
    int sair;

    const struct dicionario portugues[7] = {
    {"C", "Linguagem de programacao considerada de baixo nivel"},
    {"cafe", "Combustivel usado por programadores"}, 
    {"java", "Linguagem de programacao avancada"},
    {"computador", "dispositivo provido de hardware e software capaz de executar operacoes matematicas de alto nivel"},
    {"windows", "Sistema operacional amplamente utilizado por pessoas desprovidas de conhecimentos avancados na area de computacao"},
    {"mac", "Sistema operacional criado por Steve Jobs, o proprietario da empresa de tecnologia aplle"},
    {"pizza", "tipico aperitivo consumido por programadores durante turnos estendidos"}};

    printf("*==================================================================*\n");
   printf("|                      DICIONARIO GEEK V. 1.0                      |\n");
   printf("|                                                                  |\n");
   printf("|Autor: Luis Paulo T. Franca                                       |\n");
   printf("*==================================================================*\n\n");

    printf ("digite uma palavra: ");
    scanf ("%s", palavra);

    resultadopesquisa = procurarpalavras (portugues, palavra, NUMERODEDEFINICOES);

    if (resultadopesquisa != -1) {
        printf ("%s\n", portugues[resultadopesquisa].definicao);
    } else {
        printf ("\npalavra nao encontrada\n");
    }
    system ("pause");
}

1 answer

3


You are using the following statement:

struct dicionario {
    char palavra[21];
    char definicao[51];
};

The definitions of your entries cannot be more than 50 characters long. Now let’s count... The definition you gave for Windows is 113. If we consider the null character for termination, we have 114.

You have two options. The gross is simply to increase the size of the settings accordingly.

The elegant is to use ;) i.e pointers.:

struct dicionario {
    char* palavra;
    char* definicao;
};

You keep building the structs the same way. The "magic" here is that a pointer only points to an address in memory - in this case, a pointer to a word points to the first character. When using an index accessor, you are telling the program to access an address in the memory further and see what is there. That is to say:

palavra[0];

... Take whatever’s at the home address, but:

palavra[5];

... Take what is (size of a char in memory X five) bytes after the address of the initial character. For all intents and purposes, this takes the sixth character.

It may be necessary to make an adjustment or other in the code - it would be interesting to learn the allocation functions (search for alloc, malloc, calloc and free), for example. Any problems, ask more questions here ;)

P.S.: Working with pointers requires a little care and some dedication to learn. But if you’re going to work with C, or if you intend to go through with it beyond a college job, then you’re hopeless. Either you learn, or you change technology.

P.P.S.: when searching to answer this question, I found that searching Google for c strings is highly NSFW. Don’t look for it at work or college.

Browser other questions tagged

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