"Dereferencing NULL Pointer" error in a chained list structure

Asked

Viewed 30 times

0

My code deals with a simple endadeada list (then I will implement the pointer to turn double). But in the line strcpy_s(new_element->musica_name, musica_name); He keeps giving an elertDereferencing NULL Pointer, as I keep writing the code this alert keeps appearing, I realized that this always happens with strings so far. It does not stop the programme but I believe it is a point of attention and I do not know what to do.

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

void InserirInicio(char nome_musica[]);

struct Elementodalista_Dupla {
    char nome_musica[25];
    Elementodalista_Dupla* prox;
} * head;

int main()
{
    head = NULL;

    char nome_musica[25];

    printf("Digitie o nome da Musica: ");
    fgets(nome_musica, 25, stdin);

    InserirInicio(nome_musica);
}

void InserirInicio(char nome_musica[])
{
    Elementodalista_Dupla* novo_elemento;
    novo_elemento = (struct Elementodalista_Dupla*)malloc(sizeof(struct Elementodalista_Dupla));

    strcpy_s(novo_elemento->nome_musica, nome_musica); //Linha onde o programa traz o alerta Dereferencing NULL pointer 'novo_elemento'.

    if (head == NULL)
    {
        head = novo_elemento;
        novo_elemento->prox = NULL;
    }
    else
    {
        novo_elemento->prox = head;
        head = novo_elemento;
    }
}
  • 1

    apparently your code is ok. tested other compilers besides visual studio? (there are compilers online, but you will have to change strcpy_s to strcpy)

No answers

Browser other questions tagged

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