Q:

Seg fault when assigning an instantiated struct?

struct queens_arg {
  int board[64]; 
  int focus_idx;
};

struct queens_arg *args;
(*args).focus_idx = 0;

The code is as the following. I get a seg fault (debugged) to occur on setting focus_idx = 0. Why is this happening? How can I fix it? Thanks!

A:

The problem is that you're creating a pointer to a struct queens_arg, but you're not initializing it to point anywhere.  Consequently, when you write
(*args).focus_idx = 0;

You're following a garbage pointer, causing the segfault at runtime.
To fix this, make sure that you set up the pointer to point somewhere.  Here's how you can have it point to dynamically-allocated memory:
struct queens_arg* args = malloc(sizeof (struct queens_arg));

On a related note, you don't need to write
(*args).focus_idx = 0;

You can instead use this shorthand:
args->focus_idx = 0;

It means exactly the same thing, but is a bit easier to read.

