const char* argv

J

JKop

Why isn't:

int main(int argc, char* argv[]) { /* ... */ }


as:


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


I assume you can't edit the strings which argv points to. . . right ?


-JKop
 
S

Sharad Kala

JKop said:
Why isn't:

int main(int argc, char* argv[]) { /* ... */ }


as:


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


I assume you can't edit the strings which argv points to. . . right ?

No. This is a quote from C99 -
"The parameters argc and argv and the strings pointed to by the argv array
shall be modifiable by the program, and retain their last-stored values
between program
startup and program termination."

Sharad
 
J

JKop

No. This is a quote from C99 -
"The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored
values between program
startup and program termination."


This is a genuine question: What has that to do with C++? I take it that C99
is some sort of... other dialect of C or C++ . . . ?

-JKop
 
S

Sharad Kala

JKop said:
This is a genuine question: What has that to do with C++? I take it that C99
is some sort of... other dialect of C or C++ . . . ?

Nah...C++ is based on C90 actually. You can find references to C subclauses
in the C++ Standard. C99 is the more recent revision of the C Standard, I
made a quote from that. AFAIK C++ inherits it the same way as C does in this
regard, not very sure though.

Sharad
 
K

Karl Heinz Buchegger

JKop said:
This is a genuine question: What has that to do with C++? I take it that C99
is some sort of... other dialect of C or C++ . . . ?

It is the 1999 version of the C standard.

Hmm. The C++ standard doesn't seem to say anything about it. But since
this mechanism is pretty much equivalent in C++ to the C mechanism, I guess
that it is the same in C++.
 
M

Mike Wahler

JKop said:
Why isn't:

int main(int argc, char* argv[]) { /* ... */ }


as:


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

Because the 'argv[]' strings are modifiable.
I assume you can't edit the strings which argv points to. . . right ?

Wrong.

-Mike
 
I

Ioannis Vranos

JKop said:
Why isn't:

int main(int argc, char* argv[]) { /* ... */ }


as:


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


I assume you can't edit the strings which argv points to. . . right ?

C++98 retains C90 as a subset except of the case where otherwise is
mentioned.

Now I think that the C90 text should be included in C++98, but that's
another issue.


In C90 it is mentioned:


"Program startup"

The function called at program startup is named main . The
implementation declares no prototype for this function. It can be
defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv , though any
names may be used, as they are local to the function in which they are
declared):

int main(int argc, char *argv[]) { /*...*/ }


If they are defined, the parameters to the main function shall obey
the following constraints:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup. The intent is to supply to the
program information determined prior to program startup from elsewhere
in the hosted environment. If the host environment is not capable of
supplying strings with letters in both upper-case and lower-case, the
implementation shall ensure that the strings are received in
lower-case.

* If the value of argc is greater than zero, the string pointed to by
argv[0] represents the program name ;argv[0][0] shall be the null
character if the program name is not available from the host
environment. If the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent the program
parameters .

* The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their
last-stored values between program startup and program termination.
 
M

Mike Wahler

Michael Bruschkewitz said:
I would say, this depends on the Startup-Code. Especially, when you're
on an embedded system.

Well, if the implementation for the embedded system is not
a hosted implementation (imo typical of an embedded implementation),
yes, it depends. On such a system, A 'main()' is not required
at all. So any requirements about 'main()' refer to a hosted
implementation.

-Mike
 
M

Michael Bruschkewitz

Well, if the implementation for the embedded system is not
a hosted implementation (imo typical of an embedded implementation),
yes, it depends. On such a system, A 'main()' is not required
at all. So any requirements about 'main()' refer to a hosted
implementation.
I never needed main on the embedded system, it always used other ways to
find the startup function(s). But it would be possible.
I've my copy of the standard not at hand, so I currently don't know what
the standard says.
However, I don't think it is very recommendable to modify the strings
from argv, or the pointers inside argv. To be safe, you can't increase
the amount of memory needed for the pointers nor the strings. So, the
modification is limited to the cases where the number of the strings or
the length of the strings decreases.
I would avoid such potential pitfalls by a clear solution without
modifying argv. For example, convert argc,argv to
std::vector<std::string> and modify this. If you're allowed to use STL.
Especially, because startup is not a performance issue.
Also, it will always be possible to assign a new value to argv itself.

Regards,
Michael B.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top