I'm having this annoying bus error.
I've defined two structures, and defined two types which point to these structures.
Code:
struct node {
/* ... */
};
typedef struct node* NodeP; // NodeP points to a node.
struct graph {
/* ... */
NodeP* nodes; // Will be an array of node pointers.
};
typedef struct graph* GraphP; // GraphP points to a graph.
So, GraphP would point to a graph structure, which will have an array of pointers to node structures when initialized.
My problem is after I initialize <nodes> for a GraphP, I get a bus error when I try to put a NodeP into it.
This is how I create a GraphP:
Code:
GraphP newGraph(int length) {
GraphP newGraph = (GraphP)malloc(sizeof(GraphP));
/* Initialize other variables of newGraph. */
newGraph->nodes = (NodeP*)calloc(length, sizeof(NodeP));
NodeP testNode = newNode(); // A test node pointer.
// Set all the elements of newGraph->nodes to equal testNode.
int i;
for(i = 0; i < length; i++)
newGraph->nodes[i] = testNode; // Bus error here.
return newGraph;
}
Can someone tell what's going wrong?
Thanks.