I was having some confusions about string and NULLs. So, I read this thread on this very forum. But, I could not understand somethings...
Firstly, Is NULL an integer value 0, or something else?
Secondly,

if(strlen(str)==0)

this shows that the string is null or it shows that the first character is '\0' ?
if I assign

str[]=""

str now is a null string or has no value or contains the null character '\0' or something else ? How much space is it going to acquire?
And finally, please suggest me some good and valid methods to check for NULLs in strings using C++..

Generally NULL which is integer 0 is used in many places to denote the lack of anything. Pointers when not valid or not yet assigned are also set to NULL to indicate this.

Within strings NULL is used as you have seen to denote the end of string and is usually the check used in a loop to do something with a "string" of characters such as.

char myStr[] = "SomeStr"; //dont take this as what it does but im saying i have an array with the value "SomeStr"

while(putchar(*myStr++));
//prints each character of my string untill NULL

//easier to see example doing the same thing
for( size_t n = 0; myStr[n]!=NULL; n++)
{
   putchar(myStr[n]);
}

edit: As to the string "", it will be 1 byte long as a null terminated character array is numChars+1 for the null character.

Hope that gives you the kind of insight you were looking for, if not i pass you over to the "big guns" who will surely know somewhat more than i do.

I believe your above comparison

#
for( size_t n = 0; myStr[n]!=NULL; n++)
#
(

would return an error code (as it is doing with me).
Check it at codepad This is what I was trying to point, the NULL character ('\0') and NULL are different (maybe!!)

NULL is traditionally meant to only be used with pointers, never integers or characters because originally NULL was defined as #define NULL (char *)0 So to check for the string's terminating character it would be for(size_t n = 0; myStr[n] != 0; n++) C++ now normally defines NULL as 0, so the compilers that define it that way will not have a problem with using NULL instead of 0. However, just be aware that such a program may not compile with all compilers.

First this code will show you what type null is :

cout << "NULL type is " << typeid(NULL).name() << endl;
cout << "NULL value is " << NULL << endl;

This is the output you will probably get :

NULL type is int
NULL value is 0

Second this code :

if(strlen(str)==0)

checks if the length of the string str is 0. The function strlen returns the
length of the null terminated string passed to it.

And in code str[]="". The length of str is 0. It contains only the null-terminated character, which does not count of the actual length of str.

And last of all, avoid all of these complication by using std::string. Unless you
are doing this as practice or something.

Here is how NULL is defined by vc++ 2010 express in stdio.h Note that it's defined differently for c++ than it is for C.

/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif
Member Avatar for embooglement

NULL is generally used for pointers, and means that the memory being referred to should be at memory address 0, which is generally inaccessible, whereas the null terminator at the end of strings is one character's (a byte's) worth of zeros that isn't used for an actual character. So the following two lines are very different:

char str1[] = "";
char* str2 = NULL;

Here str1 is an actual array of characters, but the only character is the null terminator. str2 is a pointer to a character or array of characters, but it doesn't point to anything yet. If you tried to print str1, it'd print "", whereas if you tried to print str2, it'd probably give an error because it's trying to access the character array at memory address 0, which would likely return an exception, because you're not supposed to do that.

I hope that helped.

Thank you all, most of my questions have been sought by your answers... Now, the only thing left in doubt is the data space acquired..

char str1="";
char* str2=NULL;

see the link below:
codepad
Hope I am able to explain my problem..

>>char str1="";

That's wrong, most likely just a typo and is correct in the link you posted.

>>Hope I am able to explain my problem..
What problem? I though you said your questions were answered.

OK, sorry, now I understood, actually I was asking that why was the char pointer acquiring 4 bytes even when it was not pointing to anything.. but now I understood the pointer is storing the address which requires those 4 bytes.. Hope I am correct..!!

Yup, that's right. Pointers occupy memory too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.