So I basically get a run-time error with the first strncpy that is encountered: "access violation reading location" and I'm not sure why since I did allocate memory for the "addedFrame".
code:
void addFrame(link_t **list)
{
bool validFrame = true;
char frameName[MAX_NAME_SIZE] = { 0 };
char framePath[MAX_PATH_SIZE] = { 0 };
link_t* currentFrame = *list;
link_t* addedFrame = (link_t*)malloc(sizeof(link_t));
addedFrame->frame = (frame_t*)malloc(sizeof(frame_t));
// Checking if malloc was succesfull
if (!addedFrame->frame)
{
printf("Couldn't allocate memory\n");
exit(-1);
}
// If in case of the head being null
if (*list)
{
do
{
printf("Enter frame name: ");
fgets(frameName, MAX_NAME_SIZE, stdin);
// Resetting current frame back to the head
currentFrame = *list;
while (currentFrame->next != NULL)
{
if (!strcmp(frameName, currentFrame->next->frame->name))
{
printf("A frame with the entered name already exists\n");
validFrame = false;
}
currentFrame = currentFrame->next;
}
} while (validFrame == false);
currentFrame->next = addedFrame;
}
else
{
printf("Enter frame name: ");
fgets(frameName, MAX_NAME_SIZE, stdin);
frameName[strcspn(frameName, "\n")] = 0; // Removing the "\n" character and adding the terminating null
*list = addedFrame;
}
strncpy(addedFrame->frame->name, frameName, MAX_NAME_SIZE);
printf("Enter frame duration (in miliseconds): ");
scanf("%d", &addedFrame->frame->duration);
getchar(); // Clearing the buffer
printf("Enter frame path: ");
fgets(framePath, MAX_PATH_SIZE, stdin);
framePath[strcspn(framePath, "\n")] = 0;
strcpy(addedFrame->frame->path, framePath);
printf("\n");
addedFrame->next = NULL;
}
The above function is supposed to insert a new node at the end of the list with user inputted values.
EDIT Frame.h:
#ifndef FRAME_H
#define FRAME_H
#include <stdio.h>
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
#endif //FRAME_H
And linkedList.h:
#ifndef LINKEDLISTH
#define LINKEDLISTH
#include "Frame.h"
struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;
#endif