-1

I keep getting a seg fault and I want to use double pointers. I have a pointer to a pointer to a struct and I don't know how I can scan a double pointer to the pointer that points to the struct.

int main(int argc, char* argv[])
{   
    if(argc != 4) // Error checks to make sure for correct number of arguments
    {
        printf("Incorrect number of arguments\n");
        return 0;
    }

    Car* garage69;
    Car** garage = &garage69;

    int length;
    int *size;
    size = &length;

    fill_garage(garage, argv[1], size);

    return 0;
}

void fill_garage(Car** garage, char* cars, int* size)
{
    FILE* input = fopen(cars, "r");

    fscanf(input, "%d", size);

    printf("this is size %d \n", *size);

    *garage = malloc(sizeof(Car)**size);

    int i;

    for(i=0; i<*size; i++)
    {
        (*garage[i])[i].make = malloc(sizeof(char)*MAX_STRING_LEN);
        (*garage[i]).model = malloc(sizeof(char)*MAX_STRING_LEN);
        fscanf(input, "%d %s %s %d", (*garage[i]).year, (*garage[i]).make, 
          (*garage[i]).model, (*garage[i]).miles);

    }

    fclose(input);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256

1 Answers1

0
(*garage[i])[i].make = malloc(sizeof(char)*MAX_STRING_LEN);

should probably be

(*garage)[i].make = malloc(sizeof(char)*MAX_STRING_LEN);

Also, it would probably be a cleaner design to have fill_garage() return Car *, rather than returning void and using a Car ** argument to receive the return value..

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96