Reputation: 4907
I'm reading the solution to a problem in the book Cracking the Coding interview (Q 1.2). The objective there is to implement a function void revers(char* str)
in C that reverses a null terminating string.
The solution code looks something like this:
void reverse(char *str)
{
char* end=str;
char tmp;
if(str)
{
while(*end)
{
end++;
}
end--;
//code to reverse
}
}
Here, str
contains an address right? And if(str)
will only evaluate to false
if str
is 0
, right?
So what I'm saying is, is there no chance that str
will contain the address 0x0000
, thus evaluating if(str)
to false
?
Upvotes: 7
Views: 6043
Reputation: 320391
The statement
if (str)
is equivalent to
if (str != 0)
which in turn is interpreted by the compiler as
if (str != null-pointer-value-for-char-pointer-type)
I.e. it has nothing to do with "address zero" at all. The compiler is required to recognize the pointer context and perform comparison with the implementation-dependent null-pointer value for type char *
. The latter will be represented physically by an implementation-dependent address value, not necessarily zero address.
Upvotes: 1
Reputation: 122383
if(str)
is equivalent with
if(str != NULL)
or
if(str != 0)
This test is not to test whether str
has an address of 0
, it tests whether str
is a null pointer.
Note that a null pointer doesn't have to have an address 0
, the standard only ensures that a null pointer is unequal to any other non-null pointers.
C11 6.3.2.3 Pointers
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
Upvotes: 5
Reputation: 22624
Here, str contains an address right?
Yes. It is a char *
. Its value represents an address of a char
.
And if(str) will only evaluate to false if str is 0, right?
Yes.
is there no chance that str will contain the address 0x0000, thus evaluating if(str) to false?
This is perfectly possible. Just call the function with this value.
char * myCharPointer = 0;
reverse(myCharPointer);
I never use this style, though. I prefer to use NULL, which is guaranteed by the C standard to be equivalent. I prefer NULL because it is more expressive and it helps to separate ordinary integer values from addresses.
char * myCharPointer = NULL;
reverse(myCharPointer);
See also these related questions:
Upvotes: 1
Reputation: 437336
str
does indeed contain an address (it's a pointer to char
), and if(str)
will evaluate to false iff the str
is equal to the null pointer.
Note that the null pointer is not required by the standard to refer to address 0
; however, the standard mandates that a literal value of 0
when used in a pointer context must be interpreted by the compiler as the address of the null pointer -- whatever that might be.
This means that the test if(p == 0)
is guaranteed to always be the same as if(p == NULL)
. Also, the conditional if(p)
is guaranteed to always be the same as if(p != 0)
.
Conclusion: your code will always detect a null pointer, but that's not technically the same as a pointer that points to address zero (even though in practice you are going to find that it usually is).
Upvotes: 15
Reputation: 25483
There is no such chance, unless you pass a NULL
pointer to the function.
In C there was no boolean type until it has been introduced in C99. Any logic operations (including conditionals) check a value against zero, regardless its real type.
Upvotes: 2
Reputation: 3127
Theoretically 0x0 pointer is valid, but all nowadays compiler won't give you virtual address 0, because it is reserved to represent null pointer (pointer to nothing).
Some kernels could be still using it, but unless you are writing something very low level in very uncommon kernels you could assume that 0x0 is not pointing to any memory.
NULL constant was created for value for pointer not pointing anything. Now NULL has always value 0.
Upvotes: 4
Reputation: 36630
So what I'm saying is, is there no chance that str will contain the address 0x0000, thus evaluating if(str) to false?
Yes, there is: in case you are calling reverse()
with a null pointer, it will evaluate to false.
This is used as a safety net, in case it happens that reverse()
is called with a null pointer, so that the code does not access invalid memory when actually reversing the string.
Upvotes: 2
Reputation: 9474
if(str)
is added to check whether str has allocated memory or not. Best practice is to initialize a pointer to NULL
when it is declared.
the above is equal to
if(str != NULL)
{
}
Upvotes: 1
Reputation: 6606
Yes it is possible if str
is a NULL pointer on any architecture which have reserved address 0x000
for null pointer
.
Upvotes: 1
Reputation: 2146
If you are asking should I test for someone passing NULL pointers in my function then the answer is YES.
Upvotes: 1