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?
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.– Maniero
@bigown Sorry, but the code insertion tool is not the best!
[codigo]
= seems not to work for codes with more than 50lines– pcccj
It just worked with me. Look at it now. It’s extremely simple. Programming is several orders of magnitude harder than using formatting.
– Maniero
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
– pcccj
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
@hugomg This way really turned out like I wanted. Thank you
– pcccj
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.
– Maniero