Help: structs and pointers for C pointers

Asked

Viewed 67 times

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

  • 1

    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.

  • 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.

1 answer

0

I found another method to explain what I’m having trouble with. This is just an example program. Briefly I’m having trouble understanding how a pointer pointer works inside a string of pointers:

CUTE FOLDER. H:

#ifndef CUTE_H_INCLUDED
#define CUTE_H_INCLUDED
typedef struct _nome{
    char *caracter;
    int n;
}Nome;
typedef struct _pessoa{
    char sexo;
    Nome **nome;
}Pessoa;
void nome(Pessoa *pessoa);
//pega o nome e o sexo da pessoa
void fofura(Pessoa *pessoa);
/*
se o sexo for feminino, chama a pessoa de fofinha
se a sexo for masculino, chama a pessoa de fofinho
*/


#endif // CUTE_H_INCLUDED

CUTE FOLDER. C:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"cute.h"
void nome(Pessoa *pessoa){
    pessoa=(Pessoa*)malloc(sizeof(Pessoa));
    printf("você é do sexo feminino ou masculino? digite 'f' ou 'm'");
    do{
        scanf("%c",&pessoa->sexo);
        fflush(stdin);
        if(pessoa->sexo!='f'&& pessoa->sexo!='m')
            printf("\n por favor, escolha entre masculino e feminino");
    }while(pessoa->sexo!='f'&& pessoa->sexo!='m');
    printf("qual é o seu nome??");
    pessoa->nome=(Nome**)malloc(2*sizeof(Nome*));
    pessoa->nome[0]=(Nome*)malloc(sizeof(Nome));
    pessoa->nome[0]->caracter=(char*)malloc(sizeof(char));
    gets(pessoa->nome[0]->caracter);
    pessoa->nome[0]->n=2;
}
void fofura(Pessoa *pessoa){
    if(pessoa->sexo=='f')
        printf("%s, você é muito fofinha!",pessoa->nome[0]->caracter);
    else
        printf("%s, você é muito fofinho!",pessoa->nome[0]->caracter);
}

MAIN:

#include<stdlib.h>
#include<stdio.h>
#include <locale.h>
#include"cute.h"

int main(){
    setlocale(LC_ALL, "");
    Pessoa *nova;
    nome(nova);
    fofura(nova);
    system("pause");

}

Browser other questions tagged

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