- #1
camel-man
- 76
- 0
I need some help with this piece of code I wrote earlier, it is compiling correctly but when I try to use my tester program it crashes.
I am trying to create an array of pointers to struct Nodes
test program where it crashes
I am trying to create an array of pointers to struct Nodes
Code:
typedef struct Node *NodeP;
struct Node
{
NodeP next;
EmployeeP info;
};
struct EmployeeList
{
NodeP* root;
int size;
};
EmployeeListP newEmployeeList(int size)
{
//Creating employeelist and allocating memory for the array or pointers to nodes
EmployeeListP e=malloc(sizeof(struct EmployeeList));
e->root=malloc(sizeof(NodeP)* (size*2));
int i;
if(e==NULL || e->root==NULL)
{
fprintf(stderr,"Failed to allocate memory!\n");
exit(1);
}
//Initializing list size and all pointers to NULL
e->size=size;
for(i=0;i<size*2;i++)
{
e->root[i]->info=NULL;
e->root[i]->next=NULL;
}
return e;
}
test program where it crashes
Code:
#include <stdio.h>
#include <stdlib.h>
#include"Employee.h"
#include"EmployeeList.h"
int main(void)
{
EmployeeListP e= newEmployeeList(10);
return 0;
}