segmentation fault when using pointer to pointer
Posted
by
user3697730
on Stack Overflow
See other posts from Stack Overflow
or by user3697730
Published on 2014-06-04T21:23:31Z
Indexed on
2014/06/04
21:24 UTC
Read the original article
Hit count: 154
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 thw program..I get a segmentation fault..What should I do?Any help would be appreciated!
© Stack Overflow or respective owner