Pointer to pointer to const conversions

Pointer to pointer to const conversions

Post by James Agui » Fri, 23 Jun 2006 05:02:56


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.
 
 
 

Pointer to pointer to const conversions

Post by James Agui » Fri, 23 Jun 2006 05:11:49

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.

 
 
 

Pointer to pointer to const conversions

Post by Andrey Tar » Fri, 23 Jun 2006 05:18:33


See the FAQ

http://www.yqcomputer.com/ ++-faq-lite/const-correctness.html#faq-18.17
 
 
 

Pointer to pointer to const conversions

Post by Frederick » Fri, 23 Jun 2006 18:00:37

James Aguilar posted:



Here's something that was posted not so long ago:

http://www.yqcomputer.com/ ++/msg/8836b5949eb07061?hl=en&