1

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!

15
  • 1
    Don't cast the result of malloc: stackoverflow.com/questions/605845/… Commented Jun 4, 2014 at 21:26
  • 2
    The casts are both wrong and unnecessary. And you're dereferencing uninitialize data. In short, most of this is wrong. Commented Jun 4, 2014 at 21:26
  • You're receiving l2 as an argument, but you're ignoring it and setting it to point to a new structure that you get with malloc(). Commented Jun 4, 2014 at 21:29
  • 1
    It would be helpful if we didn't have to guess what you were trying to do. What is the expected outcome of this for l and l2 back in main() after the call to abc(&l, &l2) ? Commented Jun 4, 2014 at 21:31
  • 1
    In order to get some better answers, even if you are trying to learn, you should define a purpose for the abc function, for example to create the linked lists (maybe you should even change the name of the function). Commented Jun 4, 2014 at 21:57

3 Answers 3

2

Note that l and l2 are declared as pointers in main, and neither one is initialized to point to anything. So you have two choices, either initialize the pointers in main and then use them in abc, or initialize the pointers in abc.

Based on what you've written, it seems that you want to initialize in abc. To do that you must malloc enough memory to hold the struct list and then set the pointer to point to that memory. The resulting abc function looks like this

void abc( struct list **l, struct list **l2 )
{
    *l  = malloc( sizeof(struct list) );
    *l2 = malloc( sizeof(struct list) );

    if ( l == NULL || l2 == NULL )
    {
        fprintf( stderr, "out of memory\n" );
        exit( 1 );
    }

    (*l)->data = 12;
    (*l)->next = *l2;

    (*l2)->data = 34;
    (*l2)->next = NULL;

    printf( "%d %d\n", (*l)->data, (*l2)->data );
}
Sign up to request clarification or add additional context in comments.

Comments

1

The crash is due to (*l)->data=12; since (*l) is actually an unitialized varaible.

Comments

0

This part is incorrect:

 l2=(struct list**)malloc( sizeof(struct list*));
 (*l)->data=12;

You cannot assign to a structure that you didn't allocate. The correct code would be something along the lines of:

 *l = malloc(sizeof(struct list));
 (*l)->data = 12;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.