Copying char arr[64] to char arr[] can cause a segmentation fault?
Clash Royale CLAN TAG #URR8PPP Copying char arr[64] to char arr can cause a segmentation fault? typedef struct { int num; char arr[64]; } A; typedef struct { int num; char arr; } B; I declared A* a; and then put some data into it. Now I want to cast it to a B* . A* a; B* A* a; a->num = 1; strcpy(a->arr, "Hi"); B* b = (B*)a; Is this right? I get a segmentation fault sometimes (not always), and I wonder if this could be the cause of the problem. I got a segmentation fault even though I didn't try to access to char arr after casting. char arr 1 Answer 1 This defines a pointer variable A* a; There is nothing it is cleanly pointing to, the pointer is non-initialised. This accesses whatever it is pointing to a->num = 1; strcpy(a->arr, "Hi"); Without allocating anything to the pointer beforehand (by e.g. using malloc() ) this is asking for segfaults as ...