Error in vector copy of structured type

Asked

Viewed 183 times

0

Error copying elements from a vector of structured type to another vector of the same kind:

At the end of the code below, I use a function to "print" all elements of the tabela2 (vector that will receive the table content), but the prints come out "empty".

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

typedef struct dicionario
{
  char chave[21];//string de até 20 caracteres

  int* linhas;//vetor de inteiros

  int tam_linhas;//tamanho do vetor acima;

}Dicionario;

void verificaAlocacao(Dicionario* tabela)
{
    if(tabela == NULL)
    {
        printf("\nmemoria insuficiente");

        exit(1);
    }
}

void inicializa(Dicionario* tabela, int tam)//atribui para todos ->tam_linhas = 0;
{
    int i;

    for(i=0; i < tam; i++)
        tabela[i].tam_linhas = 0;
}

void reallocaTabela(Dicionario* tabela, Dicionario* tabela2, int tam, int tam_antigo)
{
    int i,
        pos=0,
        k;

    char op_efetuada;//BREAK

    for(i=0; i < tam_antigo; i++)
    {
        op_efetuada == 'N';

        if(tabela[i].tam_linhas != 0)//se tam_linahs != 0
        {

            while(op_efetuada == 'N')
            {
                strcpy(tabela2[pos].chave, tabela[i].chave);//atualiza a chave

                tabela2[pos].tam_linhas = tabela[i].tam_linhas;//atualiza o tamanho das linhas

                tabela2[pos].linhas = (int*) malloc(tabela2[pos].tam_linhas*sizeof(int));//cria o vetor de linhas

                if(tabela2[pos].linhas == NULL)//verifica alocacao
                {
                    printf("\nmemoria insuficiente");

                    exit(1);
                }

                for(k=0; k < tabela2[pos].tam_linhas; k++)//copia elemntos do vetor de linhas
                    tabela2[pos].linhas[k] = tabela[i].linhas[k];

                op_efetuada = 'S';
            }

            pos++;
        }
    }
}

void printTabela(Dicionario* tabela2, int tam)
{
    int l,y;

    for(l=0; l < tam; l++)
    {
        if(tabela2[l].tam_linhas != 0)
        {
            printf("%s",tabela2[l].chave);

            printf(" - tam_linhas: %d - vetor:",tabela2[l].tam_linhas);

            for(y=0; y < tabela2[l].tam_linhas; y++)
                printf("[%d]", tabela2[l].linhas[y]);

            printf("\n");

        }
        else
            printf("[vazio]\n");
    }
}

int main()
{
    int tam=3;//tamanho da tabela

    Dicionario* tabela = (Dicionario*) malloc(tam*sizeof(Dicionario));

    Dicionario* tabela2;

    verificaAlocacao(tabela);

    inicializa(tabela, tam);

    ///ATRIBUI VALORES PARA TABELA

        //na posicao [0]
        strcpy(tabela[0].chave, "bola");//chave = bola

        tabela[0].linhas = (int*) malloc(3*sizeof(int));//atualiza vetor de linhas
            tabela[0].linhas[0]=0;
            tabela[0].linhas[1]=1;
            tabela[0].linhas[2]=2;

        tabela[0].tam_linhas =3;//atualiza tam de linhas

        //na posicao [2]
        strcpy(tabela[2].chave, "carro");//chave = carro

        tabela[2].linhas = (int*) malloc(2*sizeof(int));
            tabela[2].linhas[0]=2;
            tabela[2].linhas[1]=9;

        tabela[2].tam_linhas =2;

    ///FIM

    tabela2 = (Dicionario*) malloc((2*tam +1)*sizeof(Dicionario));

    verificaAlocacao(tabela2);

    reallocaTabela(tabela, tabela2, (2*tam +1), tam);

    printTabela(tabela2,(2*tam +1));

    return 0;
}

What’s the matter?

  • 1

    Try to format the code correctly to make it easier for people who can help you. Use the icon {} in editing. Also note some things we do here other than forums. Here we have just questions and answers, not "conversations". http://meta.pt.stackoverflow.com/questions/846/sauda%C3%A7%C3%B5es-e-acknowledgments You’ll learn when people edit your posts the right style.

  • @bigown Sorry, but the code insertion tool is not the best! [codigo] = seems not to work for codes with more than 50lines

  • It just worked with me. Look at it now. It’s extremely simple. Programming is several orders of magnitude harder than using formatting.

  • Yes, indeed. I will try to improve on that. But I hope that in the future the user experience will increase with regard to formatting tool. Because you are not competing at the level of other forums. Thank you

  • 1

    The stackoverflow uses markdown formatting and to format the code it has to be indented 4 spaces. The easiest way to do this is to select everything and squeeze the little boot of {} in the toolbar.

  • @hugomg This way really turned out like I wanted. Thank you

  • 2

    And avoid posting the same problem over and over again. When you have to change something, you must [Dit] the question to adjust and stay so that everyone can understand. This is not a forum, edits are part of the user experience so we have good questions and answers and little or no noise.

Show 2 more comments

1 answer

1


The problem was the incongruity of some passages of the function reallocTabela();

for example in the passage: op_efetuada == 'N';, which instead of using the increment symbol(=), ended up using the symbol of equality(==);

Browser other questions tagged

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