2

Probably a conceptual mistake from my part but let's say I have a function which takes a char * as a parameter; that is, a C-style string. But I want to make sure that char * is pointing to something. So could I use something like:

foo(const char * const &cstring) 

to specify that I'm expecting cstring to be a const reference to a const char *? This way I wouldnt need to check for NULL pointers inside foo.

3
  • Step one should be trying out what you're attempting and see if it works. Commented Jan 17, 2012 at 18:36
  • 2
    FTR, C and C++ are not the same language. Commented Jan 17, 2012 at 18:38
  • 4
    Yes, a reasonable question. I wanted to do this, too, in my early days of learning C++, back when I thought I was supposed to use pointers all the time in order to work with strings. That is the conceptual mistake that you're making. Rather than defining a function that takes a char * of any kind, define a function that takes a reference to a std::string. Commented Jan 17, 2012 at 18:41

4 Answers 4

4

What you've written assures that the reference itself is bound to a valid pointer that may still be null. There isn't any compile time way to do what you want.

Your best option to to not use C-strings but take a std::string by value or const reference. If that isn't suitable then take the const char*, put in the function documentation that null is not accepted, and do a runtime assert that the pointer isn't null.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Yes I'll do that. I think this answer is the most complete.
4

This doesn't stop you from getting NULL pointers ... you're getting a reference to a pointer ... that means that the value in the memory address (or register) represented by the reference-argument at the machine-code level will be the address of a pointer, not a pointer-value itself. Thus the pointer could still have a NULL value assigned to it.

So while the argument itself may not be NULL (as it could be if the argument was a pointer), the value that the pointer is pointing to may be NULL, and therefore you will still have to check for NULL pointers.

Comments

3

You're passing a const-reference to a C string, but that will not guarentee cstring itself contains the value NULL. You still have to check cstring == NULL manually inside foo. It is like an int const& will not bother to disallow you from passing the value 0.

Comments

0

A reference is equivalent to T* const ptr_name.

The second thing is that I would say that you always have to check for null pointers.

3 Comments

References are most definitely not equivalent to T* const name. Perhaps this is the pointer type that is most similar to a reference, but it is a gross misstatement to say that they are equivalent. And of course, with references, you don't have to check for null pointers. That's the goal of the asker here.
ok, i guess you are saying that references can not be null as they have to be initialized when declared(please correct me if i'm wrong), on the other hand we can not forget about such situation: int p = NULL; int& r = *p, in this scenario reference is null, the program compiles and runs without problem (just checked this) until we want to use such reference. As far as the "equivalency" is concerned, of course T const name is NOT the same as reference, however they behave is similar manner as function parameters, other differences between pointers and ref. still holds.
Of course as every one else i want to learn more so if i'm wrong please explain me why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.