Node.h
typedef struct Node Node;
struct Node{
int rank;
int marked;
size_t nSize;
size_t nCapacity;
char * name;
Node * predecessor;
Table * weights;
void (*print)(Node * toPrint); ///< print function for printing all info
Node ** neighbors;
};
void init_node(Node ** node, char * name, void (*printNode)(Node * n));
Node.c
void init_node(Node ** node, char * name, void (*printNode)(Node * n)){
*node = (Node *) malloc(sizeof(Node));
if ( node == NULL ){
assert(NULL);
}
(*node)->rank=0;
(*node)->marked=0;
(*node)->nSize=0;
(*node)->name=name;
(*node)->predecessor=(Node *)malloc(sizeof(Node));
if ( (*node)->predecessor == NULL ){
assert(NULL);
}
(*node)->nCapacity = INITIAL_CAPACITY;
(*node)->neighbors=(Node **)calloc((*node)->nCapacity, sizeof(Node *));
if ( (*node)->neighbors == NULL ){
assert(NULL);
}
(*node)->weights = create(strHash, strEquals, strLongPrint );
(*node)->print = printNode;
}
main.c
for (size_t i = 0; i < TEST_AMOUNT ; i++){
char str[TEST_AMOUNT + 1] ="";
sprintf(str, "%zu", i);
Node * n = malloc(sizeof(*n));
init_node(&n, str, printNode);
nodes[i] = *n;
nodes[i].print(&nodes[i]);
}
printf("First: %p Second: %p\n", (void *)&nodes[0].name, (void *)&nodes[1].name);
printf("\n\nCreated an array of %d Nodes\n\n", TEST_AMOUNT);
for (size_t i = 0; i < TEST_AMOUNT; i++){
nodes[0].print(&nodes[0]);
}
So I have this node class, and I keep having this issue when I create a node then initialize it in a for loop like above then assign that value to the array. The array then contains TEST_AMOUNT number of the same node rather then a series of nodes labeled 0-TEST_AMOUNT. I don't seem to have this problem when I create a node and initialize it outside of the for loop, and was curious as to what was causing this.