1
typedef struct my_struct {
  int A;
  int B;
} my_struct;

void foo(my_struct * a) {
  a= malloc(5 * sizeof ( my_struct));
  a[1].B = 3;
}

int main(int argc, char** argv) {

  my_struct * newStruct;
  foo(newStruct);
  printf("\n%d", newStruct[1].B);
}

Trying to print newStruct[1].B results in a seg fault. What is wrong with the memory allocation here and what is the correct way to allocate the memory.

Chris
  • 574
  • 7
  • 24
Jason Per
  • 139
  • 2
  • 12

0 Answers0