|
I'm trying to use malloc to allocate memory for a couple of structures in a function. I've done my research but haven't been able to find anything that's helped me. Here are the structures:
|
Code:
typedef struct _node
{
char letter;
int num_neighbors;
struct _node *neighbors[8];
} node;
typedef struct _graph
{
int num_nodes;
struct _node *nodes; /* pointer to first element in an array of nodes */
} graph;
Here's what I've been trying:
Code:
graph* graph_create(int size)
{
node** neighbors = malloc(8*sizeof(node*));
for (int count = 0; count < 8; count++)
{
neighbors[count] = malloc(sizeof(node));
}
graph *newGraph;
newGraph = malloc(sizeof(graph));
newGraph->num_nodes = size;
return newGraph;
}