How to extend the storage capacity of char variables

2

Hello! I'm having problems with a "mini dictionary" that I'm putting together .... it's basically done, but the definition of the words, because they are too long, present an error when they are displayed. I suppose it's because of some limitation of the type of variable used. If anyone can help me I'll be grateful. Here is 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] != '
#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] != '%pre%' && palavra2[x] !='%pre%') {

        ++x;

}
    if (palavra1[x] == '%pre%' && palavra2[x] == '%pre%') {

        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] = {'%pre%'};
    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");
}
' && palavra2[x] !='%pre%') { ++x; } if (palavra1[x] == '%pre%' && palavra2[x] == '%pre%') { 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] = {'%pre%'}; 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"); }
    
asked by anonymous 31.08.2014 / 00:13

1 answer

3

You are using the following statement:

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

The definitions of your entries can not be longer than 50 characters. Now let's count ... The definition you gave to Windows is 113. If we consider the null character for termination, we have 114.

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

Stylish is to use pointers;) i.e.:

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

You continue to build the structs in 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 ahead and see what's in it. That is:

palavra[0];

... Take whatever is in the starting address, but:

palavra[5];

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

It may be necessary to make one adjustment or another 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;)

Q.: Working with hands requires a little care and requires some dedication to learn. But if you're going to work with C, or if you want to get on with it in addition to a college job, then there's no way. Either you learn, or change technology.

Q. When searching to answer this question, I found that Google search for c strings is highly NSFW. Do not look for it at work or college.

    
31.08.2014 / 01:25