473,467 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is char*[] convertible to const char*[]?

GCC 3.3 and MSVS 6.0 have no problem converting char*[] to const
char*[] (not even a warning), but MS's WinCE compiler generated an
error complained that this was not possible. MS's WinCE compiler did
allow a conversion from char*[] to const char** though.

I'm interested in what the standard would say about this. (I don't
have a copy.)

Many thanks,

Kevin

Nov 15 '05 #1
10 3244
I know that converting char* to const char* is just fine. The problem
I'm having is with arrays of strings: "char*[]" to "const char*[]". An
argument parsing library my company uses specifies "const char*[]"
because it does not change the command line parameters. But most
people write main as "int main(int argc, char*argv[])".

Some compilers convert argc to "const char*[]", but others seem
incapable. What's correct?

Nov 15 '05 #3
ke********@motioneng.com wrote:
I know that converting char* to const char* is just fine. The problem
I'm having is with arrays of strings: "char*[]" to "const char*[]". An
argument parsing library my company uses specifies "const char*[]"
because it does not change the command line parameters. But most
people write main as "int main(int argc, char*argv[])".

Some compilers convert argc to "const char*[]", but others seem
incapable. What's correct?

Slow down Kevin, and read what you're writing. Given..

int main(int argc, char *argv[])

...compilers don't convert argc at all. You seem to want to qualify the
second argument to main as..

const char *argv[]

...but it's not your call. The Standard says all that stuff is writable.
I think it's probably a bad idea to write to it but it is legal.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #4
First of all, thank you guys for trying to help me out. I really do
appreciate it. I know very well that noone here has to help me and
that they do it out of their goodwill. Thank you!
Slow down Kevin, and read what you're writing.


I hope I'm coming off quick tempered. I was just trying to clarify
things. And apparently I still haven't made things completely clear.
My company's argument parsing library has a function called ParseArgs
of the form:

xxx_error_t ParseArgs(int argc, const char*argv[]);

And from in the main function of our utilities, we write:

int main(int argc, char* argv[])
{
xxx_error_t parse_error = ParseArgs(argc, argv);

/* ... */
}

The copiler error for the WinCE compiler generated an error at the call
to ParseArgs.

I hope this clears things up. Thanks again for the help!

Nov 15 '05 #5
Joe Wright wrote:
ke********@motioneng.com wrote:
I know that converting char* to const char* is just fine. The
problem I'm having is with arrays of strings: "char*[]" to "const
char*[]".
There is a faq on this.
An argument parsing library my company uses specifies
"const char*[]" because it does not change the command line
parameters. But most people write main as "int main(int argc,
char*argv[])".

Some compilers convert argc to "const char*[]", but others seem
incapable. What's correct?
Slow down Kevin, and read what you're writing. Given..

int main(int argc, char *argv[])

..compilers don't convert argc at all. You seem to want to qualify the
second argument to main as..

const char *argv[]

..but it's not your call. The Standard says all that stuff is writable.


It depends what you mean by 'all that stuff'. The standard does not
(itself) give licence for argv[] elements to be writable, just the
strings which the elements point to. Of course, argv itself can be
modifed.

int main(int argc, char **argv)
{
if (*argv) **argv = 0; /* fine */
*argv = 0; /* undefined behaviour */
argv = 0; /* fine */
}
I think it's probably a bad idea to write to it but it is legal.


Can anyone name an implementation where they're not writable?

--
Peter

Nov 15 '05 #6
> There is a faq on this.

Do you mind pointing me to it? The only FAQ item I've ever seen is
about converting non-arrays -- something like char* to const char*.

Thanks again! =D

Nov 15 '05 #7
ke********@motioneng.com wrote:
There is a faq on this.


Do you mind pointing me to it? The only FAQ item I've ever seen is
about converting non-arrays -- something like char* to const char*.


http://www.eskimo.com/~scs/C-faq/q11.10.html

What you're probably missing is that...

int main(int argc, char *argv[])

....is identical to...

int main(int argc, char **argv)

C cannot pass whole arrays, instead it passes pointers to the
first element. Hence, function parameters declared as arrays
are silently treated as pointers.

--
Peter

Nov 15 '05 #8
Many thanks for the link! =)

Nov 15 '05 #9
ke********@motioneng.com writes:
Many thanks for the link! =)


What??

Search this newsgroup for "Context, dammit!", and follow the advice.
(You may have to go back a bit; I suspect most of the recent
occurrences are reminders like this one.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
On 26 Aug 2005 14:18:37 -0700, ke********@motioneng.com wrote:
GCC 3.3 and MSVS 6.0 have no problem converting char*[] to const
char*[] (not even a warning), but MS's WinCE compiler generated an
error complained that this was not possible. MS's WinCE compiler did
allow a conversion from char*[] to const char** though.

I'm interested in what the standard would say about this. (I don't
have a copy.)


You can't convert anything to an array in C, ever. (Although in C99 or
GNUC you can _construct_ an array value with a compound literal.)

If you're talking about passing arguments to a function parameter
declared 'const char * []', remember that such a declaration is
'adjusted' to 'const char * *'. If you try to pass a 'char * []', it
first decays to 'char * *' and then you are trying to convert that as
by assignment to 'const char * *' which isn't allowed, and should be
diagnosed by any compiler. Are you sure you are running each compiler
in a mode (i.e., with options) that tries to conform to the standard?
For gcc try -ansi (or -std=c99 if you prefer) and -pedantic and
preferably -Wall; for MSVC6 /Za /W4, or in the IDE there is a tickbox
somewhere to "disable language extensions".

_With a cast_ the pointer conversion is permitted, and should work --
that is, the conversion itself should work, since the result's target
must have the same alignment requirement, and same representation so
accessing the actual char * as a const char * must work. Using _that_
pointer might be const-unsafe in conjunction with other operations,
which is one reason the 'outer' conversion is not allowed. (This is
frequently asked, but not in the FAQ last I looked; 11.10 just says it
isn't allowed but doesn't explain why.)

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #11

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

Similar topics

5
by: Brad Moore | last post by:
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...
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...
10
by: Joachim Schmitz | last post by:
Hi folks Is it legal for a C compiler that claims to be conforming to the standard (c89) to issue an error on the following: char *foo(const char *s) { const char *s; for (s = src; *s &&...
1
by: Ted Sung | last post by:
Hi, I'm trying to call a C function from a DLL. The C function is declared as char * function( const char * ) I'm declaring it in C# as follows:
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 *'...
10
by: lovecreatesbea... | last post by:
C stops the conversion from (char **) to (const char **). c-faq.com sec 11.10 has explanation on this point. But, for example, even the conversion from (char *) to (const char *) brings the same...
11
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
6
by: mihai | last post by:
Hi, I have a question regarding the conversion (in c or c++) from char** to const char**. The fallowing code refuses :) to compile with g++ (and others): char **p; const char **q =...
10
by: LuxOcculta | last post by:
Hello, I need some help.. I have to replace in s1, the string s2 with the string s3. I first found if s2 was a substring in s1, but i dont know what to do from here, I'm lost.. example: s1 =...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.