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?

perror
  • 7,071
  • 16
  • 58
  • 85
GrowinMan
  • 4,891
  • 12
  • 41
  • 58

10 Answers10

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

Jon
  • 428,835
  • 81
  • 738
  • 806
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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
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.

Ari
  • 3,101
  • 2
  • 27
  • 49
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.

Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
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.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
1

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

DuncanACoulter
  • 2,095
  • 2
  • 25
  • 38
  • 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 '13 at 08: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. – DuncanACoulter Jul 22 '13 at 08:55
1

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

Dayal rai
  • 6,548
  • 22
  • 29
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)
{

} 
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
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:

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765