I’m trying to understand why the assignment g.node = n1; isn’t possible.
Can anyone explain?
The idea is to create a graph with nodes using structures. I thought this method would work, but I get error: incompatible types in assignment for g.node = n1;
#include <stdio.h>
typedef struct
{
int value;
int *edges;
int *adj;
} Node;
typedef struct
{
Node *node;
} Graph;
void resize_array(char *, int);
void copy_array (char *, char *);
int main()
{
Graph g;
Node n1, n2;
int edgesS[1] = {9};
int adjS[1] = {5};
n1.edges = edgesS;
n1.adj = adjS;
n1.value = 1;
g.node = n1;
return 0;
}
void resize_array(char * array, int size){array[size] = '\0';}
g.nodeis of typeNode*butn1is of typeNode. The assignment would be possible asinstead. Note that
g.nodemerely points to aNode; it doesn’t contain the memory for one. With the above simple assignment, whenn1goes out of scope, the memoryg.nodepoints to becomes invalid.