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

K

kevin.hall

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
 
K

kevin.hall

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?
 
J

Joe Wright

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.
 
K

kevin.hall

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!
 
P

Peter Nilsson

Joe said:
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?
 
K

kevin.hall

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
 
P

Peter Nilsson

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.
 
K

Keith Thompson

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.)
 
D

Dave Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top