I had been trying to use a pointer to pointer in a function,but is seems that I am not doing the memory allocation correctly... My code is:
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
struct list{
int data;
struct list *next;
};
void abc (struct list **l,struct list **l2)
{
*l2=NULL;
l2=(struct list**)malloc( sizeof(struct list*));
(*l)->data=12;
printf("%d",(*l)->data);
(*l2)->next=*l2;
}
int main()
{
struct list *l,*l2;
abc(&l,&l2);
system("pause");
return(0);
}
This code compiles,but I cannot run the program..I get a segmentation fault..What should I do?Any help would be appreciated!
l2as an argument, but you're ignoring it and setting it to point to a new structure that you get withmalloc().landl2back inmain()after the call toabc(&l, &l2)?abcfunction, for example to create the linked lists (maybe you should even change the name of the function).