0

To put this in context, I'm looking at question 23 of Quiz 0 Fall 2014 (http://cdn.cs50.net/2014/fall/quizzes/0/key0.pdf)

(Don't click the link if you don't want to see the solution! The question is how to implement strlen() )

My solution is almost identical to the correct solution, the main difference being I have while (ptr != NULL) instead of while (s[n] != '\0').

Is there a difference between using NULL and using the 'null-character'?

1 Answer 1

1

NULL is expanded from a macro (a preprocessor #define), which is usually defined as:

#define NULL ((void*)0)

which means that it's defined as a pointer. Basically, it's the same thing. When you declare a pointer as NULL, you're actually storing the value 0 in it, so it's basically the same thing. Underneath the hood 0, '\0' and NULL all have a value of 0.

The main difference between what you're doing and what the solution does is that you seem to be using pointer arithmetics and the solution uses array indexes. The latter seems a bit clearer to me, but the end result is the same for both.

1
  • 1
    has already answered this perfectly, but just to wrap up, technically, NULL and '\0' are one and the same. they both refer to the same value 0, but in different contexts if you could say. namely, these contexts are a memory address and a char value respectively in this case.
    – kzidane
    Oct 31, 2015 at 21:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .