I'm writing a small program that takes a set of mathematical expressions (x^2, sin x, etc.) from a text file and plots them onto a graph, when reading each expression they are stored into a struct declared as follows:
typedef struct Expression
{
int r, g, b;
char* expression;
} Expression;
Note: the r, g, and b variables are the colour of the line that will be plotted, the expression is stored as a string.
My linked list and node are declared as:
typedef struct LinkedListNode
{
struct Expression *expression;
struct LinkedListNode *next;
} LinkedListNode;
typedef struct LinkedList
{
LinkedListNode *head;
} LinkedList;
All I do to read the expressions in is run a while loop until EOF (I know all this works fine):
Expression* expression = (Expression*)malloc(sizeof(Expression));
LinkedList* expressionList;
expressionList = createEmptyList();
while(!feof(readFile))
{
fscanf(readFile, "%d %d %d %[^\n]s", &r, &g, &b, expr);
fgetc(readFile);
expression -> r = r;
expression -> g = g;
expression -> b = b;
expression -> expression = expr;
insertNodeFront(expressionList, expression);
}
Now, the insertNodeFront function is where I'm having problems:
void insertNodeFront(LinkedList *inList, Expression *inExpression)
{
LinkedListNode *newNode;
newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
newNode -> expression = inExpression;
newNode -> next = inList -> head;
inList -> head = newNode;
}
I think it's this line in particular where the issue lies:
newNode -> expression = inExpression;
Because each expression member of every linked list is still pointing to the original Expression struct they will all end up with the same value in the end (obviously problematic), so I assume I have to create a duplicate of the inExpression pointer within insertNodeFront but I'm not exactly sure how to do this, all my attempts at using memcpy have resulted in crashes (although this is my first time using memcpy so I'm probably doing something wrong).
Any advice would be helpful, Thanks