-4
void fill_garage(Car** garage, char* cars, int* size)
 43 {
 44     int i;
 45     FILE* file=fopen(cars,"r");
 46     fscanf(file,"%d",size);
 47     *garage=malloc(sizeof(Car)**size);
 48     printf("%d",*size);
 49     for(i=0;i<*size;i++)
 50     {
 51         (*garage)[i].make=malloc(sizeof(char)*MAX_STRING_LEN);
 52         (*garage)[i].model=malloc(sizeof(char)*MAX_STRING_LEN);
 53         fscanf(file,"%d%s%s%d",(*garage)[i].year,(*garage)[i].make,(*garage)[i].model,(*garage)[i].miles);
 54     }
 55     fclose(file);
 56 }

I am getting a segmentation fault on this code, gdb returns that the garage[i]->make line is the reason for this but I can not figure out why this line would case a seg fault.

  • `garage=&carsPtr;` should be `*garage = carsPtr;` . You want to tell the calling function about `carsPtr`. – M.M Feb 12 '15 at 03:31
  • check the return value of `fscanf` . Check `*size` is what you think it is. You never allocated any memory to `carsPtr[i].model` either. – M.M Feb 12 '15 at 04:31
  • Basically you need to *debug your code*. If you can't use a debugger then use `printf` statements to make sure everything is as it should be at each step of the program. – M.M Feb 12 '15 at 04:31
  • also the `equals` function is wrong; you want to check if the two strings have the same letters in them; not check if they both inhabit the same memory address. – M.M Feb 12 '15 at 04:32
  • In `drive_cars` `fscanf(file,"%d%s%s%d",&year[i],&make[i],&model[i],&miles[i]);` is wrong, think about what `&make[i]` is. You have so many serious errors in this code, I think you need to scale down and start smaller. Get a basic version working and free of errors first, and then gradually add new functionality. – M.M Feb 12 '15 at 04:34
  • [Please don't cast the result of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Quentin Feb 12 '15 at 23:39
  • You have just edited the question to change the code. Is there still a problem? – M.M Feb 13 '15 at 03:08
  • yes I can not figure out why this is seg faulting in the fill_garage function. Gdb keeps saying that it is the garage[i]->make line. – Brandon Tomblinson Feb 13 '15 at 03:22
  • @BrandonTomblinson if you are unsure about the file format, read from the file using `fgets()` then use `sscanf()` to retrieve information from the line, that way it is easier to control the input. – AndersK Feb 13 '15 at 05:23
  • OP, completely deleting your previous code versions is bad manners since it renders answers incomprehensible. –  Feb 13 '15 at 05:28
  • Does it output the value of `*size` correctly? –  Feb 13 '15 at 05:46
  • Yes it does now I am working on another part where I have logic errors. Thank you for your help. – Brandon Tomblinson Feb 13 '15 at 05:53

2 Answers2

1

fscanf with %d format requires a pointer to int as an argument. You are apparently trying to pass the int itself instead of a pointer

fscanf(file,"%d%s%s%d", 
  &(*garage)[i].year,
  (*garage)[i].make,
  (*garage)[i].model,
  &(*garage)[i].miles);

Note the placement of & operators in arguments of fscanf.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

You are wrong in using garage[i]->. You should be using (*garage)[i]. instead. With garage[i], you're not traversing the allocated array, but rather the memory in direct proximity to the garage pointer, and then trying to write it, which most probably causes the segfault.

And now you have changed your code to pass values instead of pointers to fscanf. You should use &(*garage)[i].year and &(*garage)[i].miles.

  • but when its called in main and then passed by reference it doesn't get initialized then?? Also when it gets called in the first fscanf before the loop it reads an integer value out of the file – Brandon Tomblinson Feb 13 '15 at 04:54
  • @BrandonTomblinson: if it doesn't get initialized, it can very well have even a negative value, since using an uninitialized variable is *undefined behaviour*. Even if this isn't what caused the error, you shouldn't be doing it. –  Feb 13 '15 at 04:57
  • Where would I have to initialize it then? Because the file that is opened has it on the first line – Brandon Tomblinson Feb 13 '15 at 04:58
  • @BrandonTomblinson: Wherever you like to, it's your code. Oh, and I found another error, I'll update my post now –  Feb 13 '15 at 04:59
  • Hmmm well it isn't causing the seg fault because when I explicated declared it as a value and commented out the code and bumped the file pointer so it would skip the size line it still seg faulted – Brandon Tomblinson Feb 13 '15 at 05:04
  • Well garage is a double pointer, so I would need to have a go between for garage and the actual structs correct? Like something for garage to point to which would then be all the structs – Brandon Tomblinson Feb 13 '15 at 05:10
  • I have uploaded the updated code however it is still seg faulting – Brandon Tomblinson Feb 13 '15 at 05:19
  • @BrandonTomblinson: are you sure you've properly modified your `fscanf` code? –  Feb 13 '15 at 05:22
  • It is still seg faulting I originally had the address operator on the last 2 struct arguments in the fscanf. How would I properly modify then? – Brandon Tomblinson Feb 13 '15 at 05:29
  • Not the *last two*, should be the first and the last. @AndreyT's answer has the whole code for the call. –  Feb 13 '15 at 05:31
  • Yeah my bad that is what I meant I updated it to that still seg fault. – Brandon Tomblinson Feb 13 '15 at 05:33