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!

nhgrif
  • 61,578
  • 25
  • 134
  • 173
user3697730
  • 251
  • 1
  • 3
  • 13
  • 1
    Don't cast the result of malloc: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar Jun 04 '14 at 21:26
  • 2
    The casts are both wrong *and* unnecessary. And you're dereferencing uninitialize data. In short, *most* of this is wrong. – WhozCraig Jun 04 '14 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()`. – Barmar Jun 04 '14 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)` ? – WhozCraig Jun 04 '14 at 21:31
  • Actually,at this code,there is absolutely no real purpose...I do it only for practice reasons!I don't want to have any outcome in main() at this code...I do want to have an outcome though,at a larger code that I am trying to make,when I do expect from my function to return a few things...Right now,I do it just for practice,because I noticed that I encountered that segmentation fault at the larger program... – user3697730 Jun 04 '14 at 21:36
  • I tried *l = malloc(sizeof(struct list)); (*l)->data = 12; – user3697730 Jun 04 '14 at 21:37
  • But still,it didn't work...Thank you for your time though! – user3697730 Jun 04 '14 at 21:38
  • For some reason, *l = malloc( sizeof(struct list) ); *l2 = malloc( sizeof(struct list) ); do not compile...! – user3697730 Jun 04 '14 at 21:45
  • 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). – Andrei Bozantan Jun 04 '14 at 21:57
  • @user3697730 instead of saying "do not compile", post the actual error messages you get. – M.M Jun 04 '14 at 21:59
  • If you don't want to have any outcome in main, then make it `void abc(void)` and declare the pointers inside `abc`. But you say that you are testing something, so you should at least have some code in `main` to perform that test after `abc` has run, to check that `abc` worked as you were expecting. – M.M Jun 04 '14 at 22:00
  • Ok...I get " invalid conversion from `void*' to `list*' " – user3697730 Jun 04 '14 at 22:01
  • @user3697730 that means you are using a C++ compiler. Since this is a C program, you should use a C compiler instead. Most (all?) C++ compilers come bundled with a C compiler. – M.M Jun 04 '14 at 22:03
  • The actuall purpose of my larger program was to create a list,sort it,create another list by using it and then print the output.I managed to make a correct code without functions...But when I tried to do the same thing with functions,I encountered the problem I mentioned...So you can assume that abc is a function that must create a list and then return it to main()! – user3697730 Jun 04 '14 at 22:05
  • Well,you are right!!I was using a c++ compiler by mistake...!I am really a fool...!I hadn't even noticed it!It works now!!Thank you!! – user3697730 Jun 04 '14 at 22:07

3 Answers3

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 );
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
1

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

Andrei Bozantan
  • 3,781
  • 2
  • 30
  • 40
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;
user4815162342
  • 141,790
  • 18
  • 296
  • 355