I have the following data structure:
typedef struct node
{
int key;
int *v;
}node;
... and the global variables:
node *multiway[10];
int contor=0;
I am trying to insert inside this structure all the nodes of a multiway tree, and all the kids of each node. In order to do this, I made this function:
int * getKids(int value,int n) //returneaza vectorul de copii ai unui nod
{
//value-nodul parinte
//n- numarul de noduri ale vectorului
int *result=(int*)malloc(n*sizeof(int));
int counter=0;
int i;
for(i=1;i<=n;i++)
{
if(a[i]==value)
{
counter++;
result[counter]=i;
printf("%d ",result[counter]);
}
}
int copii[counter]; //in vector pun toti copiii valorii date, value
for(i=1;i<=counter;i++)
{
copii[i]=result[i];
}
contor++;
multiway[contor]=(node*)malloc(sizeof(node)); //added this line after a comment
multiway[contor]->key=value; //SEGMENTATION FAULT
multiway[contor]->v=copii;
return result;
}
My code compiles with no warnings, but when I run, it crashes. When I debug, I get a segmentation fault at the line which I commented with "Segmentation fault". Any idea of what I did wrong? Thank you.