?
Solved

empty string

Posted on 2003-02-26
6
Medium Priority
?
189 Views
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...
or

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

or
if(!str)

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??

0
Comment
Question by:pigyc
6 Comments
 
LVL 12

Accepted Solution

by:
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...
0
 
LVL 1

Expert Comment

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

Quote:

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.



0
 
LVL 12

Expert Comment

by:rajeev_devin
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...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:pigyc
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)
0
 

Author Comment

by:pigyc
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;
}PERSON_RECORD

void foo(PERSON_RECORD *array, int num_record){
PERSON_RECORD *item;

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.....    


}
}
0
 
LVL 6

Expert Comment

by:gj62
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...
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

621 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