0

I'm trying to return a pointer to a linked list to main so that I can pass it around to other functions.Anytime I try to get load to pass anything the program crashes and it messes up the file output.I just can't figure out why.

#include <stdio.h>
#include <stdlib.h>

struct list{                //creating linked list
 char furn[128];
 int priority;
 struct list *next;
           };
 typedef struct list List;   //typedef for list
              //creating pointer for head of list

List **Load();
int main(List **points)

{
  points->Load();               //crashes here no if I try to set Load to anything, works fine and prints okay if I don't





}
List ** Load()

{    List *head;
     List *current;                        //so we can build and navigate list
    List *new;
    FILE *fin, *fout;                     //so that we can start file manipulation
    char name[]= "California.txt" ;
    fin=fopen(name, "r");
    new=malloc((sizeof(List)));    //creating list
    head=new;
while(current->next!=NULL)
    {
        current=new;
        new=malloc((sizeof(List)));
        fscanf(fin,"%s %d",current->furn,&current->priority);    //reading and populating list
        current->next=new;


    if(feof(fin))
        current->next=NULL;

   }
   current=head;
   printf("%s %d",current->furn,current->priority);
 while(current->next!=NULL)
   {
        printf("Starting");
       printf("%s %d",current->furn,current->priority);
       current=current->next;
   }
   List **points=&head;
   return points;    //Returning so we can have a pointer to the list

   }
  • 5
    I am not really into C, but does `int main(List **points)` work? – pzaenger Dec 06 '15 at 00:10
  • The points variable goes out of scope and gets de-allocated when the method exits. You can't return it. – nicomp Dec 06 '15 at 00:11
  • `points->Load();` shouldn't even compile, no matter how permissive your compiler settings. This isn't C (it's not C++ either). If it does somehow compile, the crash is most likely because `points->` is trying to dereference the integer `1` (or a similar small number that isn't a valid address), because the first argument to `main` is a small integer on any normal OS. – Alex Celeste Dec 06 '15 at 00:31
  • 1
    Please indent your code! – chqrlie Dec 06 '15 at 00:32
  • Having `Load()` return a `List **` seems implausible; a `List *` is more orthodox, at least. In the function, you have a local (automatic) variable `List *head;` and at the end you have `List **points=&head; return points;` which means you're returning the address of a local variable, which is always a bad idea. Change the function to return `List *` and simply use `return head;` would give you a fighting chance if everything else was reasonably kosher. The definition of `main()` as it stands is anything but kosher; the use of `points->Load()` in `main()` is not going to work. – Jonathan Leffler Dec 06 '15 at 01:50
  • Thank you, yes I messed up with the code there, I was trying some things out but left it unfinished. Originally I did have it points=Load(); yet that still gave me weird printing values. Even if I change the variable it returns I get quite strange values. How do I make the linked list global if that's the problem? I really just want to return the head of the linked list so I can pass it around again to other functions. – lizardbrush Dec 06 '15 at 17:18

2 Answers2

1
int main(List **points)

does not work. It should be:

int main(int argc, char *argv[])

See main(int argc, char *argv[]) or What should main() return in C and C++? for more details.

points->Load();   

does not work - if your function Load returns a pointer then it should be:

points=Load();

I think there is also an issue with you repeated use of malloc in the loop in the function. It seems to me that you want a long list, but I think it will be impossible to free all the memory you have allocated at the end of the program, because you only return the address of head.

Community
  • 1
  • 1
tom
  • 1,303
  • 10
  • 17
  • That is a good time to explain when you allocate memory, you have two responsibilities (1) preserving a pointer to the starting address for the allocated block of memory so, (2) it can be freed when no longer in use. In a linked-list, you expect to see an allocation with each node creation, so `malloc` within a loop is fine, but you have to manage the pointers properly to insure you are not violating rule (1). That is where the errors begin in the code above. – David C. Rankin Dec 06 '15 at 01:49
  • This certainly covers some key points, though by no means all. – Jonathan Leffler Dec 06 '15 at 01:54
  • well even if I change points=Load(); it gives me an error. When it prints in Load after I set it equal then I get random address variables instead of strings and integers. – lizardbrush Dec 06 '15 at 17:17
  • @lizardbrush I am afraid the points=Load() is not the only issue with your code as noted in the comment by Jonathan Leffier this answer covers some of the key points that need to be addressed - hope it is helpful. Note the comment by David Rankin above about memory allocation and keeping track of pointers to memory allocated.. – tom Dec 07 '15 at 00:06
0

I figured out an answer. I wasn't allocating memory in main so something was happening to it when I tried to pass the pointer. After I allocated it in main I was able to create a double pointer that points to the address of the first pointer and then pass that around. Manipulating the linked list by dereferencing my double pointer (*pointer)->item. Thanks for the help I got the idea from here.