Hi, I have a small doubt in C/C++ with respect to structs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23  | struct A
{
  char *name; 
};
struct B
{
  char name[5];
};
void write_ptr(char *name)
{
  A *a = (A *) malloc(sizeof(A));
  a->name = name;
  fwrite(a, sizeof(A), 1, f); // f is file pointer
}
void write_cpy(char *name)
{
  B *b = (B *) malloc(sizoof(B));
  strcpy(b->name, name);
  fwrite(b, sizeof(B), 1, f); // f is file pointer
}
 
 | 
 
I ran this code and opened the output file, with write_cpy I could see the name written, but not with write_ptr the strange thing is when reading back the file into struct both works fine, how is this happening? will file produced by write_ptr be readable if distributed?