1

I'm fairly new at C and I was wondering how to set all elements in an array of pointers to null. I created a structure that includes a pointer to an array of pointers, and I'd like to set all of those pointers to null.

Here is the structure:

typedef struct
{
    char **array;
    int size, capacity;
} ArrayList;

And here's where I try to create an array of pointers and initialize them to null:

ArrayList *createArrayList(int length)
{
    int i;
    ArrayList* strArray = malloc(sizeof(ArrayList));
    for(i=1; i<length; i++)
    {
    strArray->array[i] = NULL;
    }
return strArray;
}

Unfortunately this yields a segmentation fault. Any help would be greatly appreciated.

4 Answers4

3
strArray->array[i] = NULL;

You can't do this as you have not allocated memory for array. You need to allocate it as

strArray->array = malloc(sizeof(char *) * length);

And then initialize, all elements to NULL as you have done.

You can also use calloc that set memory to 0, in which case you don't need the for loop.

strArray->array = calloc(length, sizeof(char *));
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • I agree in general that `strArray->array = calloc(length, sizeof(char *));` is sufficient. A robust solution would use `for(i=0; iarray[i] = NULL;` as `NULL` is not always 0.. That deals with some computer in a forgotten hall of some tech building. – chux - Reinstate Monica Feb 02 '14 at 05:02
  • Oh derp, not sure how I overlooked that. Thanks a lot! – user3262014 Feb 02 '14 at 05:10
  • @chux: The *null pointer constant* (which the `NULL` macro is defined to be) is always 0-valued (6.3.2.3/3). – John Bode Feb 02 '14 at 05:16
  • @John Bode Further detail in http://stackoverflow.com/questions/8288482/is-null-in-c-required-defined-to-be-zero/8288517 – chux - Reinstate Monica Feb 02 '14 at 05:24
  • 1
    @chux: I'm not sure if you and John Bode are disagreeing or not, but think you two were talking about different things. It is true that the internal representation of the null pointer need not consist of all zero bits, so calloc is not guaranteed to work here. But you wrote in your comment that "NULL is not always 0", and that sounds like the NULL macro in the C source code, which _is_ (a possibly casted) 0. But maybe it's just too early in the morning for me to understand the comments. – Thomas Padron-McCarthy Feb 02 '14 at 06:40
  • @Thomas Padron-McCarthy To get back on point. Is "space is initialized to all bits zero" done by `calloc()` such that `strArray->array[i] == NULL`? Certainly it will be so on many platforms, but all? Are the exceptions, if any, of concerning in 2014? – chux - Reinstate Monica Feb 02 '14 at 07:09
  • 1
    @chux: Someone who actually knows anything about exotic modern hardware (read: anything not x86) needs to answer that. But one justification for being careful could be that we don't know what the null pointers will look like in the future, when our decades-old code needs to run on virtual quantum cloud computers. (Or even nuclear virtual quantum cloud turbo-computers, which is what 64-bit threaded programs on multi-core processors would have seemed to me back in the eighties.) – Thomas Padron-McCarthy Feb 02 '14 at 08:52
1

First of all,

ArrayList* strArray = malloc(sizeof(ArrayList));

only allocates space for the strArray instance itself; it does not allocate anything for your array member. You will have to allocate that memory separately:

strArray->array = calloc( length, sizeof *strArray->array );

calloc will zero-initialize all the elements of the array.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

You have to allocate memory for your array of pointers as well as the struct. And you should start your loop at 0 since C arrays are 0-based.

ArrayList *createArrayList(int length)
{
    int i;
    ArrayList* strArray = malloc(sizeof(ArrayList));
    strArray->array = malloc(length * sizeof(char *));
    for (i = 0; i < length; i++)
    {
        strArray->array[i] = NULL;
    }
    return strArray;
}
ooga
  • 15,423
  • 2
  • 20
  • 21
0

They are already null. You are getting the seg fault because you can't assign to a variable that hasn't been allocated yet. from the line

strArray->array[i] = NULL;

will
  • 312
  • 3
  • 9