empty string

Posted on 2003-02-26
Medium Priority
Last Modified: 2010-04-15
pigy is not clear about the concept of null pointer and emtpy string.
I saw codes using something like

if(str !=NULL)  doSomething...

if(str[0] !='\0')


or combine both using
if(str!=NULL && str[0] !='\0')

could any one clear this concepts and let me know when to use what? should i always use
if(str!=NULL and str[0] !='\0') to be safer??

Question by:pigyc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

rajeev_devin earned 200 total points
ID: 8024768
Let me explain you the concept one by one

1. if(str !=NULL)

is generally used with dynamic memory allocation to check whether memory is allocated to str. str!=NULL means that memory is allocated for str. When you allocate memory in stack, there is no need to use this statement.

2. if(str[0] !='\0') is used to check whether the first element of the array is NULL or not. '\0' and NULL are the same.

3. if(!str) statement is quite same as statement1. If str is NULL the if condition is executed. Otherwise not.

4. if(str!=NULL && str[0] !='\0') is used to both the things 1 and 2.

If you don't find anything clear, add your comments again.
Hope this may help you...

Expert Comment

ID: 8025155
#2 is a little confusing (I think) and a little inaccurate.  


2. if(str[0] !='\0') is used to check whether the first element of the array is NULL or not. '\0' and NULL are the same.

'\0' == (char) 0.
NULL means a pointer to nothing.
(anytype *) 0 gets coverted to NULL by the compiler.

anyway checking if a char == NULL does not make any sense although it may compile in VC it won't on some platforms (esp Solaris, I think) or at least they complain.  

short answer: do this

if(str!=NULL && str[0] !='\0') just as rajeev said.

Also give the points to him I think he explained it well except for this subtle point.

LVL 12

Expert Comment

ID: 8025544
Reply to frogger1999:

The character '\0' is internally represented as 0, and the macro NULL also has the value 0. You can check it out. I am not speaking in terms of compiler. In different compilers we may have to type cast the thing also.

Hope this may clarify the thing further...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8026498
am i getting it right?explain a bit of stack, please.
so if i define something in a local function like this:

char str[256];
then i am allocating memory in statck? so i just need to check
if(str[0] !='\0')
  strcpy(str, someRealNonEmtyString) ??

if I do malloc for str and put data record by record into array of str, then use
if(!str && str[0] !='\0')  ??

i am assuming if have something like this,
char str2[256];
char str="hello";
char *ptr = str,
then would it be enough to check this ptr is not null(if(!ptr), or do i need to put
if(!ptr && ptr[0]!='\0')
  strcpy(str2, ptr)

Author Comment

ID: 8027126
one more thing to be clarified:
(excuse me: i purposely use *item to clear my cloudy part)

typedef struct person_record{
char *name;
char *id;
int   age;

void foo(PERSON_RECORD *array, int num_record){

for(int i=0; i<num_record; i++){
   item = &(array[i]);

   if(item->name != NULL && item->name[0]!='\0')
/*do i need to use both of above to make sure name is empty??*/
     do somehting.....    


Expert Comment

ID: 8027562
Empty is different than NULL -

A string is empty if string[0]==0; or string[0]=='\0';, whichever you prefer.

If string==NULL, then string is uninitialized - the pointer is not valid.

If string is NULL, then the statement string[0]==0, or any other reference, will cause an exception...

So if it might be set to NULL, you need to test for it.

Lastly, realize that you've allocated no space to the strings in your struct - you will need to do that with malloc, or by setting the pointer equal to an existing string, etc...

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question