Pointer to pointer to const conversions

J

James Aguilar

Quick question. Why, in the following code sample, can I not convert a
char ** to a const char **?

void test(const char **p)
{
cout << str(*p) << '\n';
}

int main()
{
char *str = new char[10];
char **pstr = &str;

test(pstr);

return 0;
}

My error output is:

1>------ Build started: Project: test, Configuration: Debug Win32
1>Compiling...
1>test.cpp
1>test.cpp(3) : error C2065: 'cout' : undeclared identifier
1>test.cpp(3) : error C3861: 'str': identifier not found
1>test.cpp(11) : error C2664: 'test' : cannot convert parameter 1
from 'char **' to 'const char **'
1> Conversion loses qualifiers
1>Build log was saved at "file://xxxxxx\Debug\BuildLog.htm"
1>test - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped

Any help would be appreciated. I'm sure someone must have brought this
issue up before, but it's not easy to search for, let's put it that way.
 
J

James Aguilar

Corrected code:

#include <iostream>
#include <string>

using namespace std;

void test(const char **p)
{
cout << string(*p) << '\n';
}

int main()
{
char *str = new char[10];
char **pstr = &str;

test(pstr);

return 0;
}

Sorry about that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top