473,608 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22022
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.ohi o-state.edu> wrote in message
news:ck******** **@news.cis.ohi o-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********@boe ing.com.invalid > wrote in message
news:I5******** @news.boeing.co m...
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
11522
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' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
6
16039
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 PushUnique(int); for(y = 0; y < 9; y++)
3
6280
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? Thanks
3
16794
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 *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
3
13926
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, const void *args, OSCTimeTag when, NetworkReturnAddressPtr returnAddr) { const char *typeArgs = (const char *)args;
1
10319
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: 'CreateFileW' : cannot convert parameter 1 from 'const char ' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2
9151
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
14965
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; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, "http://www.thescripts.com", -1, buff, sizeof(buff)); CppWebBrowser1->Navigate(buff, 0, 0, 0, 0); } To include the address bar implementation, I need a temporary string derived from an edit box. This is...
5
5383
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()); i have also attached searchTree below for yr reference
0
8063
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8003
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.