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

   }
6
  • 5
    I am not really into C, but does int main(List **points) work? Commented Dec 6, 2015 at 0:10
  • The points variable goes out of scope and gets de-allocated when the method exits. You can't return it. Commented Dec 6, 2015 at 0: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. Commented Dec 6, 2015 at 0:31
  • 1
    Please indent your code! Commented Dec 6, 2015 at 0: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. Commented Dec 6, 2015 at 1:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
This certainly covers some key points, though by no means all.
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 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..
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.

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.