I have a structure Player, which serves as a linked list.
I'm trying to initialize the 'name var' portion of Player. I'm receiving an error: invalid conversion from 'void*' to 'char*'.
I don't understand why this is happening since i've malloc memory and then just want to copy the string over.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Player
{
char *name;
int jersey;
int points;
struct Player* next;
};
int main()
{
struct Player *head, *ptr;
head = (struct Player*)malloc(sizeof(struct Player));
char playerName[] = "Hurley";
head->name=malloc(strlen(playerName)+1);
strncpy(head->name, "Hurley");
head->jersey = 11;
head->points = 15;
head->next = NULL;
ptr = (struct Player*)malloc(sizeof(struct Player));
char playerName2[] = "Hill";
ptr->name=malloc(strlen(playerName2)+1);
strncpy(ptr->name, "Hill");
ptr->jersey =33;
ptr->points = 17;
ptr->next = NULL;
head->next=ptr;
printf("head name: %s, next name: %s\n", head->name, ptr->name);
free(head);
free(ptr);
}