0

I have a function:

void add(char const**);

And I invoke it as follow:

template<typename T>
void anotherAdd(T const& t) {
  add(&t);
}
...
anotherAdd("some string");

As a result I get the error:

 no known conversion for argument 1 from 'const char (*)[10]' to 'const char**'     

Why the conversion can't be made?

Because I think the following is true:

"some string" <=> char const* =>
&"some string" <=> char const**
4
  • 1
    Why have you made a template function that relies on a function that only works on a char const **? I you want this to work, you will have to cast T const& t to a char const **. Commented Mar 18, 2013 at 14:26
  • 1
    You will need to explain what you are actually trying to achieve, since what you are doing in the code doesn't really make sense. Commented Mar 18, 2013 at 14:31
  • Are you asking how to get your code to work, or are you asking why that conversion can't be made? Commented Mar 18, 2013 at 14:32
  • I removed my downvote, since you kind of salvaged your question with the last edit. Commented Mar 18, 2013 at 14:37

1 Answer 1

1

Arrays are not pointers.

This code expects a pointer to a pointer

void add(char const**);

The compiler is telling you that it can't produce a pointer to a pointer because your code has no pointer to point to. You're effectively trying to evaluate &"some string", which has no valid meaning.

This code will work, because it creates the missing char const* that you're trying to take the address of.

template<typename T>
void anotherAdd(T const& t) {
  char const *pt = &t; // Now there's a pointer that you can take the address of.
  add(&pt);
}
Sign up to request clarification or add additional context in comments.

3 Comments

And what is &"some string"? is not it char const**? Because const char[] is const char *.
@user14416 "const char[] is const char *" No, it isn't. Arrays are not pointers. You're getting an error because you're assuming that if you have an array, you have a pointer variable somewhere.
Ok, the follwoing answer (cplusplus.com/forum/articles/10) says that it is true.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.