7

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?

9
  • Can I refer you to an answer of mine here regarding a similar question?
    – Nobilis
    Jul 22, 2013 at 8:37
  • 1
    Not unless you yourself pass str as zero to the function. Jul 22, 2013 at 8:37
  • 6
    @ludesign Ah, the joy of undefined behavior... Jul 22, 2013 at 8:38
  • 2
    why not: reverse(0) Jul 22, 2013 at 8:38
  • 6
    @ludesign Even if a single compiler does it in some situations, unspecified behavior is by definition unspecified and should not be trusted upon. Jul 22, 2013 at 8:41

10 Answers 10

15

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

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

3

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.

2

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.

2

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.

4
  • 1
    How do you know that reverse() is not called with a 0-pointer? Jul 22, 2013 at 8:37
  • Of course, I mean if you pass a valid pointer. I will fix an answer to clarify. Jul 22, 2013 at 8:38
  • Ooops, it seems that I misunderstood the question. Sorry for that. Jul 22, 2013 at 8:39
  • in C99 boolean type was introduced
    – triclosan
    Jul 22, 2013 at 8:42
1

If you are asking should I test for someone passing NULL pointers in my function then the answer is YES.

2
  • I disagree. It's not the function responsibility to check whether it gets valid parameter or not. If someone made a "wrong call" by passing a NULL pointer, well, let the program crash. Of course, this heavily depends on context, so it may not apply to the poster's situation.
    – Xaqq
    Jul 22, 2013 at 8:49
  • @Xaqq it would depend on whether the function was intended to be called directly by other programmers or serves only as an internal helper function. When in doubt err on the side of caution. Jul 22, 2013 at 8:55
1

Yes it is possible if str is a NULL pointer on any architecture which have reserved address 0x000 for null pointer.

1

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)
{

} 
1

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:

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.