c - 如何解决段错误(核心转储)?

标签 c algorithm segmentation-fault coredump

我有这段代码,我不知道为什么之后,我问你是否想介绍另一个学生,我说1或0程序结束并说段错误(核心转储)。

我要求介绍_nodo *insertaEnLista中的另一位学生

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

struct actividades
{
  char tipoDeActividad[22];
  char diaDeLaSemana[12];
  char horaDeIncio[8];
  char horaDeFin[8];
};

struct materias
{
   char nombre[30];
   char profesor[30];
   char tipoDeMateria[20];
   struct actividades *actividad;
};

struct alumnos
{
   char nombre[30];
   int cedula;
   int telefono;
   struct materias *materia;
   struct alumnos *siguiente;
};

typedef struct alumnos _nodo;

_nodo *crearLista(_nodo *apuntador);
bool listaVacia(_nodo *apuntador);
_nodo *insetarEnLista(char nombre[], long  cedula, long  telefono, _nodo *apuntador);
void imprimirLista (_nodo *apuntador);
_nodo *crearNodo(char nombre[], long int cedula, long int telefono);

//AQUI SE CREA LISTA Y SE PONE PARA QUE APUNTE A NULL
_nodo *crearLista(_nodo *apuntador)
{
    return (apuntador = NULL);
}

//ESTA FUNCION VERIFICA SI LA LISTA ESTA VACIA 
bool listaVacia(_nodo *apuntador)
{
    if (apuntador == NULL)
        return (true);
    else
        return (false);
}

//AQUI SE CREA EL NUEVO NODO DE LA LISTA

_nodo *crearNodo(char nombre[], long cedula, long telefono)
{
    _nodo *registroNuevo;

    registroNuevo = (_nodo *) malloc(sizeof(_nodo));

    printf("\n----NUEVO ELEMENTO----\n");
    printf("NOMBRE: ");
    fflush(stdin);
    scanf("%s",nombre);
    printf("CEDULA: ");
    fflush(stdin);  
    scanf("%ld", &cedula);
    printf("TELEFONO: ");
    fflush(stdin);  
    scanf("%ld", &telefono);
    fflush(stdin);

        strcpy(registroNuevo->nombre, nombre);
        registroNuevo->cedula = cedula;
        registroNuevo->telefono = telefono;
        registroNuevo->siguiente = NULL;

    return registroNuevo;   

}

//AQUI SE INSERTA EL NODO EN LA LISTA LUGEO DE SER CREADO POR LA FUNCION crearNodo
_nodo *insetarEnLista(char nombre[], long  cedula, long  telefono, _nodo *apuntador)
{
    _nodo *registroNuevo, *apuntadorAuxiliar;
    char respuesta,ch;

    do
    {

            registroNuevo=crearNodo(nombre, cedula, telefono);
            if (listaVacia(apuntador)) apuntador = registroNuevo;
            else
            {
                apuntadorAuxiliar = apuntador;
                while (apuntadorAuxiliar->siguiente != NULL)
                    apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
                apuntadorAuxiliar->siguiente = registroNuevo;
            }

            printf("\nPARA INGRESAR A OTRO ALUMNO MARQUE... 1");
            printf("\nPARA SALIR MARQUE... '0'\n");         
     while((ch = getchar()) != EOF && ch != '\n');      

    scanf("%c", &respuesta);
        fflush(stdin);
   printf("RESPUESTA = %c", respuesta);             

    }while (strcmp(&respuesta, "1")==0);
    return apuntador;
}

//IMPRIMIR LOS NODOS DE LA LISTA
void imprimirLista (_nodo *apuntador)
{
    _nodo *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntador;

    if (apuntador == NULL)
        printf("NO HAY ELEMENTOS EN LA LISTA \n");
    else
    {
        while(apuntador != NULL)
        {
            printf(" \n------------NODO-------------- ");
            printf("\nNOMBRE: %s \n\n", apuntadorAuxiliar->nombre);
            printf("\n\nCEDULA: %d \n", apuntadorAuxiliar->cedula);
            printf("\nTELEFONO: %d \n", apuntadorAuxiliar->telefono);

            apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
        }
    }

    return;
}

int main()
{
    /*printf("INTRODUZCA LOS NUMEROS DE CEDULA QUE DESEA IMPRIMIR \n");*/

    _nodo *inicioLista;
    int cedula;
    int telefono;

    char nombre[20];

    inicioLista = crearLista(inicioLista);

    inicioLista = insetarEnLista(nombre, cedula, telefono, inicioLista);

    imprimirLista(inicioLista);

    return 0;
}

我该如何解决这个问题。

最佳答案

您应该在调试器中单步调试代码,并查看每一步的变量以确定导致问题的代码行。

这是一个问题,可能还有其他问题。

在这一行

}while (strcmp(&respuesta, "1")==0);

您正在将 strcmp 与包含单个字符的变量 (respuesta) 一起使用。 strcmp 需要一个以 null 结尾的字符串(末尾有零字节的字符数组)。由于变量后面可能没有零字节,这可能会导致 strcmp 读取不应该读取的内存(这是缓冲区溢出)

使用起来更简单:

}while (respuesta == '1');

关于c - 如何解决段错误(核心转储)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40732452/

相关文章:

java - 获取具有子集的列表的 "adjacent"值的算法

查找包围点的多边形的算法 - 仅定义线

algorithm - 最大公约数 - 上限

c++ - 多态成员变量

c - 返回指针时出现段错误

c - 返回指针的函数中的段错误

c - 数组指针数组上的指针

c++ - GNU 使 : customize library search path in Makefile

c - off_t without -D_FILE_OFFSET_BITS=64 on a file > 2GB

c - C中的字符串格式化和解析