By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Pointer to pointer to const conversions

P: n/a
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.

Jun 21 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jun 21 '06 #2

P: n/a
James Aguilar wrote:
Quick question. Why, in the following code sample, can I not convert a
char ** to a const char **?
...


See the FAQ

http://www.parashift.com/c++-faq-lit...html#faq-18.17

--
Best regards,
Andrey Tarasevich
Jun 21 '06 #3

P: n/a
James Aguilar posted:
Quick question. Why, in the following code sample, can I not convert a
char ** to a const char **?

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

http://groups.google.ie/group/comp.s...eb07061?hl=en&
Jun 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.