2016-11-22 3 views
0

나는이 코드를 가지고 왜 그런지 모르겠다. 다른 학생을 소개하고 싶은데 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; 
} 

에 다른 학생을 소개 부탁드립니다.

답변

0

디버거에서 코드를 단계별로 실행하고 각 단계의 변수를 살펴보고 문제의 원인이되는 코드 줄을 확인해야합니다.

여기에는 한 가지 문제가있을 수 있습니다. 당신은 하나의 문자가 포함 된 변수 (respuesta)과 strcmp를 사용하는이 라인

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

에서

. strcmp은 널 종료 문자열 (끝에 0 바이트의 문자 배열)이 필요합니다.

}while (respuesta == '1'); 
: 당신이 변수 후 0 바이트가 없을 수 있습니다,이는

훨씬 간단

그냥 사용하기 (이 버퍼 오버런이) 안된다는 strcmp와 메모리를 읽을 될 수 있습니다