Yoel Marcu
Yoel Marcu

Reputation: 1

Assign a pointer to point to a struct

I'm trying to make an array of structs made according to user's input. first of all - I initialise an array using calloc:

  BusLine* head = calloc (num_of_lines, sizeof (BusLine));

Then in a loop I try to create structs according to input (read bus is the function that makes the structs):

BusLine* create_list(BusLine* head, size_t num){
  for (int i = 0; i<num; i++){
    BusLine bus = read_bus();
    *(head+i) = bus;
  }
  return head;
}

But when I try to do that I always end up with a SIGSEGV. According the best of my knowledge - it is possible to pass a variable to a calloced pointer, so I am struggeling with understanding the problem here.

Upvotes: 0

Views: 39

Answers (0)

Related Questions