0

I have to initialize a string array consisting only of a NULL pointer, nothing else. If I understand correctly assigning a NULL pointer looks like this:

char **array = NULL;

Otherwise, I tried

char *array[] = {NULL};

But the space for the NULL pointer should be dynamically allocated and here I'm confused, my code doesn't work.

char **array = (char**)malloc(sizeof(char*));

I would be really grateful if you could help me with this.

3
  • 2
    There is a difference if the pointer is null or points to a (memory location containing) null. Which one do you want? Commented Apr 13, 2015 at 14:52
  • 2
    @BLUEPIXY No, calloc() can't portably do this. Also, don't cast the return value of malloc() and friends in C. Commented Apr 13, 2015 at 14:56
  • The pointer should be null. Commented Apr 13, 2015 at 14:57

3 Answers 3

5

This should do it:

char **array = malloc(1 * sizeof *array);
array[0] = NULL;

This assumes the allocation succeeds, you should test that before relying on it in actual code. The 1 means "array of 1 element", it's of course redundant but I felt it added some clarity.

The above declares a pointer to (an array of) character pointers, and tries to allocate room for 1 such character pointer.

Actually doing this seems very pointless, but I can't read your mind to figure out what you probably should be doing. :)

Sign up to request clarification or add additional context in comments.

9 Comments

what is the '1' for in the malloc size parameter? it seems to be totally unneeded and is similar to the often seen code that uses 'sizeof(char)' as part of the malloc size parameter.
@user3629249 Of course multiplying by 1 is never needed, I included it for clarity and to show how I thought. It's the size (in elements) of the array we're allocating room for. I edited.
well, sizeof(char) is nonsense in the vast majority of compilers, but sizeof(wchar_t) not. One must ask, What's the meaning of having a neutral element in a mathematical set if it doesn't affect the result of its operations? At least, you will make your code more portable, as it will compile on archs where sizeof(char) happens to be != 1. A C manual from IBM described the use of constants in programs using an example based on MATH_PI (and claimed for the easyness of changing the value of the constant, case of MATH_PI would change in the future)
@LuisColorado sizeof (char) is 1 in all compliant C compilers. And all of that is totally besides the point, this is allocating one character pointer, not one character.
@unwind, well, my response was not for you, it was for @user3629249, who talked about sizeof(char) in his. But being 1 in all compliant C compilers doesn't mean a compliant compiler could not exists where sizeof (char) is not one. Don't flame me for that, my comment wasn't addressed to you. Normally, using 1 * in an expression makes the code more readable, and I agree with your answer. I have even upvoted it.
|
5
  • char **array = NULL; is not an array, it is a pointer-to-pointer. For some reason this is a common misunderstanding. There is no relation between a pointer-to-pointer and arrays. In particular, a pointer-to-pointer is not a 2D array.

  • char *array[] = {NULL}; this is an array of pointers with 1 array items.

  • char **array = (char**)malloc(sizeof(char*)); This is very common but bad practice. Do not use this method, you'll create a fragmented look-up table splattered all over the heap.

You should use this method to allocate a true multi-dimensional array on the heap. Once you have done that, you can memset() the whole array to zero, if NULL is implemented as zero on your system (in the odd case where it isn't, you'll have to set every item of the array manually through a loop instead).

As a side note, Do I cast the result of malloc?

Comments

0

A NULL pointer will always have the size of a pointer. You can't change the size of a pointer.

The first two methods you tried are both ok. You're creating a variable of type char ** and then assigning it the value NULL. You can't change the size of that variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.