I'm new to C and this is my first question: for the this structure:
typedef struct Branch
{
Tree * thisTree;
struct Branch * nodes[];
} Branch;
it seems to work fine if I do the following:
Branch branch1;
branch1->nodes[0] = NULL;
even if I do not allocate memory for the pointer nr 0 in the array this way:
branch->node[0] = (Branch *) malloc(sizeof(Branch *));
if i check with this code:
if ( branch1->nodes[0] == NULL)
printf("is NULL");
it prints to the output: is NULL
So my question is: has there been allocated memory for the pointer?
branch1->nodes[0]
because I have a lot of structures and if I initialise each branch with a fixed number of pointers I get a lot of allocated data (if I check with the sizeof function). Is this way: setting to NULL (above) a wrong way of thinking ?
My problem is that the allocation of memory for a pointer is 4 bytes. So not having a declared number of pointers in the array, when does it allocate memory for it ? Sorry I tried to keep the question simple but I need to reach a string through the structure pointer in the next branch
this means that the struct I use is
typedef struct Branch
{
Tree * thisTree;
char *string;
struct Branch * nodes[];
} Branch;
So if I do not
branch->node[0] = (Branch *) malloc(sizeof(Branch *));
and than
branch->node[0]->String = strdup("text");
I ge a compiler error.