Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is char*[] convertible to const char*[]?

Reply
Thread Tools

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

 
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      08-26-2005
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

 
Reply With Quote
 
 
 
 
kernelxu@hotmail.com
Guest
Posts: n/a
 
      08-27-2005
there are some conches:

http://groups.google.com/group/comp....e9846578e62750

 
Reply With Quote
 
 
 
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      08-27-2005
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?

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-27-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 ---
 
Reply With Quote
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      08-29-2005
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!

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      08-29-2005
Joe Wright wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      08-29-2005
> 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

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      08-29-2005
(E-Mail Removed) 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

 
Reply With Quote
 
kevin.hall@motioneng.com
Guest
Posts: n/a
 
      08-30-2005
Many thanks for the link! =)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-30-2005
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM



Advertisments