0

I attempted to initialize char array with NULL like this syntax.

char str[5] = NULL;

But it returned error.. How can I initialize char array with NULL or 0 in C++?

Concretely, I want to print "Hello" in this example code.

#include <iostream>
int main()
{
 char str[5] = NULL;
 if(!str)
  std::cout << "Hello" << std::endl;
 return 0;
}

This code will return error because of incorrect initialization. Then, what initializing sentence should I replace sentence with?

5
  • Arrays aren't pointers. What exactly do you want to achieve? Commented Aug 23, 2018 at 8:37
  • You could have char *str = NULL; Are you planning to modify str later or it remains NULL? Commented Aug 23, 2018 at 8:40
  • 1
    With if(!str) you are checking if the pointer to str is NULL not if the content is 0x0 Commented Aug 23, 2018 at 8:40
  • the array will not be 0, as it will decay to pointer and it will have a memory address. <you could initialize the array with {}> or you could use a pointer, that may have a bit more sense Commented Aug 23, 2018 at 8:51
  • if(!str) - will never be true. str is an automatic variable with a determinate, non-null address. It does not have, and never will have, an expression value of NULL. You need something that can hold NULL, and that is a pointer. char *str = NULL;, for example. Commented Aug 23, 2018 at 9:06

4 Answers 4

12

An array can not be null. Null is state of a pointer, and an array is not a pointer.

In the expression !str, the array will decay to the address of the first element. The type of this decayed address is a pointer, but you cannot modify the decayed pointer. Since the array is never stored in the address 0 (except maybe in some special case on an embedded system where an array might actually be stored at that memory location), !str will never be true.

What you can do, is initialize all of the sub-objects of the array to zero. This can be achieved using the value-initialization syntax:

char str[5]{};

As I explained earlier, !str is still not meaningful, and is always false. One sensible thing that you might do is check whether the array contains an empty string. An empty string contains nothing except the terminator character (the array can have elements after the terminator, but those are not part of the string).

Simplest way to check whether a string is empty is to check whether the first character is the terminator (value-initialized character will be a terminator, so a value initialized character array does indeed contain the empty string):

if (!str[0]) // true if the string is empty
Sign up to request clarification or add additional context in comments.

5 Comments

When I initialized integer pointer with NULL, it worked well. This is an example. <code> #include <iostream> int main() { int *a; a = NULL; if(!a) std::cout << "Hello" << std::endl; return 0; } </code> Only char array's name can't be initialized with NULL?
@favdew it has nothing to do with char, and everything to do with the difference between an array, and a pointer. A pointer stores a memory address. The address zero is the null pointer. An array is a sequence of objects in memory. It does not store an address.
@user2079030 As I know, array's name has memory address pointing its first value's address in C Language. Is it different in C++?
@favdew It is not so in C++, nor in C.
@favdew That has never been true. Although, it is a common misconception.
3
//...
char str[5] = {'\0'};
if (str[0] != '\0')
//...

If you now put some characters into str it will print str up to the last '\0'. Every string literal ends with '\0', you must make sure your array ends with '\0' too, if not, data will be read beyond your array (until '\0' is encountered) and possibly beyond your application's memory space in which case your app will crash.

You should use std::string or QString however if you need a string.

19 Comments

You don't need C++ 11 for this syntax. It would even work in C.
Every const char* ends in '\0' would be better stated something like every C style string end in '\0'. There's nothing about const char* that says it has to be used for C style strings.
True, you don't need C++ 11 for that initialization. You only need it when using a vector or other collection, not a plain array.
I would amend the comment by @john to say that every string literal (rather than "C-style string" or const char *) ends in '\0'.
@JaMiT John's comment is more complete. A string literal is just a special case of C-string.
|
1

You can't initialise a char array with NULL, arrays can never be NULL. You seem to be mixing up pointers and arrays. A pointer could be initialised with NULL.

You can initialise the chars of your array with 0, that would be

char str[5] = {0};

but I don't think that's what you're asking.

Comments

-3
#include <stdio.h>
using namespace std;

int main() {

char str[5];
for(int i=0;i<5;i++){
    str[i]=NULL;
}
printf("success");
return 0;
}

Hope this helps.

4 Comments

str[i]=NULL is wrong. NULL is a macro for a pointer literal. You shouldn't use it to initialize a character. Some hull pointer literals are character literals too (like 0), but others are not (nullptr), so this is not guaranteed to work.
I tried to run the above code and then gave 'h e l l o' as input to str and it works fine on my machine.I also run the same on codechef ide and it works fine there as well. codechef.com/ide
I never did say, that it would be guaranteed to not work. I said that it's not guaranteed to work. There is a difference. Surely you would like your program to compile on all standard compliant compilers, not just the one on your machine and on codecef?
All you've done is set each char to 0. Surely not your intention, nor does it answer the question. Without a more precise definition of what you mean by "it works fine", this is not the answer.

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.