Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

0xC0000005: Access violation reading location 0x00000008. C Code

im currently making a project for a college work and while doing it i came across that error and i have no clue what is causing it.

The line it points to is : if ((*compara)(ptr->dados, valor))

The Function :

void * pesquisar(PNO cabeca, int (*compara)(), void* valor) {

    PNO ptr;
    for (ptr = cabeca; ptr; ptr = ptr->prox);
    if ((*compara)(ptr->dados, valor))
        return ptr->dados;
        return NULL;

}

The Structs

    struct no{
    struct no*prox;
    struct no*ant;
    void *dados;
};

    typedef struct no NO;
    typedef NO*PNO;
    typedef NO**PPNO;

    struct cliente{
        int ID_cliente;
        char nome[60];
        char morada[255];
        int contribuinte;
        struct cliente * prox;
    };
    typedef struct cliente CLIENTE;
    typedef CLIENTE*PCLIENTE;
    CLIENTE *cabCliente, *cauCliente;

The Main :

PNO Cliente = NULL, Fatura = NULL;
    PCLIENTE pesquisa;

    char n;
    void * Dados;
    int opcao;
    char nome[60];

case 3:
                printf("Insira o Nome: ");
                fgets(nome, 60, stdin);
                nome[strlen(nome) - 1] = '';
                pesquisa = pesquisar(Cliente, cmpNomeCliente, nome);
                if (pesquisa) printf("Nome é: %s", nome);
                else printf("Aluno inexistente");
                getch();
                break;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for (ptr = cabeca; ptr; ptr = ptr->prox);
if ((*compara)(ptr->dados, valor))

In this sequence of code, ptr in the second line will always be NULL because the for loop runs until the ptr expression is false and due to the ; after the for, the loop has no body.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...