472,789 Members | 1,285 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

cannot convert char** to const char**

Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

int main(int argc, char* argv[])
{
foo(argv);
return 0;
}

Error: cannot convert parameter 1 from char** to const char**
I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

int goo(const int x) { return 0; }
int main(int argc, char* argv[])
{
int x;
goo(x);
}

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).

Thanks for your expertise and time,
-Brad
Jul 22 '05 #1
5 21727
Brad Moore wrote:
Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

int main(int argc, char* argv[])
{
foo(argv);
return 0;
}

Error: cannot convert parameter 1 from char** to const char**
You need to consider the meaning of "**" - in this case it means a
pointer to a pointer to a [const] char. This is a double level of
indirection which allows the pointer to be modified in one place and the
object to be modified in another.

If the conversion from char** to const char** was allowed, then you
could write this code.

const char * A[2] = { "A", "B" };
char * B[2];

int main()
{
char ** b = B;
const char ** a = b; // illegal
//const char ** a = ( const char ** ) b; // BAD work-around (1)
//const char * const * a = b; // better option (2)

a[0] = A[0]; // illegal if you use (2) ok if you use (1)

b[0][0] = 'X'; // const violation - big probs if you use (1)
}

This would implicity allow const objects to be accessed in a non-const way.



I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

int goo(const int x) { return 0; }
int main(int argc, char* argv[])
{
int x;
goo(x);
}
This is a very different issue, the earlier one had a level of
indirection, this one has none. In this case x is "copied" to a const
value parameter.

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).


What you did was probably not what you wanted to do.
Jul 22 '05 #2

"Brad Moore" <mo****@cis.ohio-state.edu> wrote in message
news:ck**********@news.cis.ohio-state.edu...
Hey all,

I'm getting the following compiler error from my code. I was wondering if
anyone could help me understand the concept behind it (I actually did try
and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

int main(int argc, char* argv[])
{
foo(argv);
return 0;
}

Error: cannot convert parameter 1 from char** to const char**
I tried using an explicit type cast, and it worked. However, I'd like to
know the difference between the above code and the following (which did
compile):

int goo(const int x) { return 0; }
int main(int argc, char* argv[])
{
int x;
goo(x);
}

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is bad
form in some way, please let me know.


OK, first thing to note is that foo(char* argv[]) is just another way of
writing foo(char** argv). C and C++ allows you to use array notation in
function parameters but its a lie. The compiler always converts to array to
a pointer. So its better to use the pointer notation, its more truthful.

C++ says its ok to convert char* to const char* because this it harmless.
char* is a pointer to char, and const char* is a pointer to constant char
(note its the char that is constant not the pointer). Adding a const can't
do any harm. So sometimes people think that char** should be convertible to
const char** but this conversion is most definitely not harmless. Here's why

const char a = 'a'; // this is a constant it should never change
char* x;
const char** y = &x; // in reality this is illegal
*y = &a; // now x points to a
*x = 'b'; // now we have modified a

By allowing a conversion from char** to const char** we've managed to write
a sequence of statements which have modified a constant. This is obviously a
bad thing

john


Jul 22 '05 #3

I want to thank everyone who responded. I understand the error in my
reasoning.

Thanks,
-Brad
Jul 22 '05 #4
John Harrison wrote:

OK, first thing to note is that foo(char* argv[]) is just another way
of writing foo(char** argv).
This is true.
C and C++ allows you to use array
notation in function parameters but its a lie. The compiler always
converts to array to a pointer. So its better to use the pointer
notation, its more truthful.


This is your personal opinion, not an objective fact. Many people
prefer the other notation, and their programs are none the less for it.

Brian Rodenborn

Jul 22 '05 #5

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I5********@news.boeing.com...
John Harrison wrote:

OK, first thing to note is that foo(char* argv[]) is just another way
of writing foo(char** argv).


This is true.
C and C++ allows you to use array
notation in function parameters but its a lie. The compiler always
converts to array to a pointer. So its better to use the pointer
notation, its more truthful.


This is your personal opinion, not an objective fact. Many people
prefer the other notation, and their programs are none the less for it.


OK, well perhaps we can at least agree that people should be aware of the
real meaning of the array notation in a function parameter. Its my
experience in this group that some newbies are not.

John
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Abhijit Bhadra | last post by:
Hi , I was trying to build my project in VC with latest Service Packs but got this error . C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE\atlconv.h(125) : error C2440: 'return' :...
6
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
3
by: Alfonso Morra | last post by:
Hi, I'm using a third party library which has a function that expects an arg of type const char*. I have a char* which is assigned a value at run time - how can I pass it to my function? ...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
3
by: bg_ie | last post by:
Hi, I am using a API I downloaded from the internet programmed in C. I need the function below which works with this api in my c++ file - void StoreNoteCallback(void *context, int arglen,...
1
by: asenthil | last post by:
Hai this is senthil... I had tried to write a string which fetched from a database. into a file... when i tried to compile the solution the following error occurs like this error C2664:...
2
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
1
codexparse
by: codexparse | last post by:
I am writing a browser program in C++ Builder 6 that loads a web page using the following code: void __fastcall TForm1::ToolButton1Click(TObject *Sender) { wchar_t buff; ...
5
by: slizorn | last post by:
well the error i get is the title above: error C2664: 'searchTree' : cannot convert parameter 2 from 'const char *' to 'char' error is form this line searchTree(treeObj->root ,data1.c_str());...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.