Question: I'm trying to modify elements of an array in a function. This works fine when kept in main but when I port it to a function it segfaults after accessing the first member of the array. 

The code below is just a reduced version of my actual code to show where I'm getting the segfault.

```
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    unsigned short id;
} voter;

void initialise(voter** votersPtr, unsigned short *numOfVoters) {
    *votersPtr = malloc(sizeof(voter)*(*numOfVoters));

    for(int i = 0; i < *numOfVoters; i++) {
        votersPtr[i]->id = (unsigned short) i;
        printf("%hu \n", votersPtr[i]->id);
    }
}

int main(void) {
    unsigned short numOfVoters = 480;
    voter* voters = NULL;

    initialise(&voters, &numOfVoters);

    return EXIT_SUCCESS;
}

```

Any help would be greatly appreciated, thank you.

Answer: 
The type `voter **` is ambiguously either a *pointer to an array of pointers to `voter` objects* or a *pointer to a pointer to an array of `voter` objects*. Your code is using it as the first, but should be using it as the second. Change

```
votersPtr[i]->id = ...

```

to

```
(*votersPtr)[i].id = ...

```

and everything should work.
Remember that `votersPtr` is a pointer to a pointer of an array.

So shouldn't the following line:

```
votersPtr[i]->id = (unsigned short) i;

```

Instead be:

```
(*votersPtr)[i].id = (unsigned short) i;

```