0
Hello! I need some help! I’ve been trying for a while to do a pointer study for pointers in structures but so far I haven’t been able to reach a conclusion (it always gives "segmentacion fault"). Can anyone help me with or link examples or talking how I can get there?? Ex of the struct I’m using:
typedef struct _item{
int conta; //contador
char *termo; //palavra
}Item;
typedef struct _mapa{
int total; //número de itens no mapa
int blocos; //números de blocos de itens alocados
Item **lista; //vetor de ponteiro de itens
}Mapa;
void inicia_mapa(Mapa *mp){
mp=(Mapa*)malloc(sizeof(Mapa));
mp->blocos=0;
mp->lista=(Item**)malloc(10*sizeof(Item*));
for(int i=0;i<10;i++){
mp->lista[i]=(Item*)malloc(sizeof(Item));
//mp->lista[i]->termo=(char*)malloc(sizeof(char));
mp->lista[i]->termo=NULL;
mp->lista[i]->conta=0;
printf("\ndentro de lista:%c\nnumero principal:%d",mp->lista[i]->termo,i);
}
mp->total=0;
}
void insere_termo(Mapa *mp, char* s){
int cont=0,teste=0;
while(cont<(10*mp->blocos-1)&&teste==0){
printf("aqui");
if(mp->lista[cont]->termo=="3"){
printf("aqui");
mp->lista[cont]->termo=(char*)malloc(sizeof(char));
strcmp(mp,s);
mp->lista[cont]->conta=1;
mp->total=1;
teste=1;
}
else
cont++;
}
if (teste==1)
return;
else{
mp->blocos++;
}
}
Note: the "here" served to vera where the error was, so please do not tell it. Grateful
– Fulwondel Irmage
It is not so (if(mp->list[cont]->term="3"){) that one compares strings in C, use the strcmp function of <string. h>. Here you are allocating memory for a single character, I believe you want to allocate memory for a string, that is, a string. This (strcmp(mp,s);) makes no sense, maybe you wanted to use the strcpy function but still, the parameters would not be these.
– anonimo
Beyond what @anonimo said, it’s worth explaining what you’re trying to do with the code. We can’t tell what’s wrong if we don’t know what he intends to do.
– Isac