Is it OK to modify argv ???

L

Larry__Weiss

Richard said:
The standard gives explicit permission to modify argv itself and
the strings pointed to by argv[0] through argv[argc - 1], but it doesn't
give permission to modify argv[0] through argv[argc - 1] themselves.

Strange that you can modify the strings, considering that they may
in fact be null strings. How can you modify a null string?

I would have found it more reasonable that you would have to leave the
original strings alone, but have the freedom to allocate and substitute
different strings in the argv array, or in fact allocate a new argv structure
and change argv to point to the new one.

On rereading the Standard (from 5.1.2.2.1 Program startup)

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.

I can imagine that that means my alternative interpretation without too much stretch.

It might mean not that the content of the original strings are modifiable, but
that the strings themselves can be substituted for other strings.

Where are the examples? The Standard should be overflowing with examples
of code that reinforce what the prose text says.

- Larry Weiss
 
A

Al Bowers

Richard said:
The Real OS/2 Guy wrote:

char *temp = argv[2];
argv[2] = argv[1];
argv[1] = temp;

What's the story on that?

It is portable.


I disagree. The standard gives explicit permission to modify argv itself and
the strings pointed to by argv[0] through argv[argc - 1], but it doesn't
give permission to modify argv[0] through argv[argc - 1] themselves.
Here is what the standard says:

"
_
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.
"

This might be interpeted as as argv and its parameters,
argv[0] (program name), and argv[1] through argv[argc-1]
collectively called program parameters, are modifiable.

My opinion is that is only safe to modify argv in the
matter of k&r.
while(--argc > 0)
{
printf("%s%s", *++argv, (argc>1)?" ":"");
printf("\n");
}

I have never felt it safe to modify by enlarging the strings
pointed to by argv nor modify argv[0] thru argv[argc].
 
R

Richard Heathfield

This might be interpeted as as argv and its parameters,
argv[0] (program name), and argv[1] through argv[argc-1]
collectively called program parameters, are modifiable.


No, it explicitly names the argv object itself, and the strings pointed to
by the argv array. It says nothing about the members of the argv array
themselves.
My opinion is that is only safe to modify argv in the
matter of k&r.
while(--argc > 0)
{
printf("%s%s", *++argv, (argc>1)?" ":"");
printf("\n");
}

I have never felt it safe to modify by enlarging the strings
pointed to by argv nor modify argv[0] thru argv[argc].

On the enlarging-strings side, neither have I. In fact, I don't believe
there's a guaranteed-safe way to do this. I think you're okay with argv[0]
though.
 
L

Larry__Weiss

Richard said:
On the enlarging-strings side, neither have I. In fact, I don't believe
there's a guaranteed-safe way to do this. I think you're okay with argv[0]
though.

Again, I'll say that if the authors of the C Standard would take the time
to create and publish some actual examples, then we all could be much more
assured in what we "think" we know about C.

- Larry Weiss
 
D

Dave Thompson

Richard said:
The standard gives explicit permission to modify argv itself and
the strings pointed to by argv[0] through argv[argc - 1], but it doesn't
give permission to modify argv[0] through argv[argc - 1] themselves.

Strange that you can modify the strings, considering that they may
in fact be null strings. How can you modify a null string?
A null string is an array (or sequence) of 1 char containing '\0'.
You can store any char value there, although if nonzero the result can
no longer be used as a string. This isn't terribly useful, in fact
it's probably not useful at all, but it is well-defined and legal.
I would have found it more reasonable that you would have to leave the
original strings alone, but have the freedom to allocate and substitute
different strings in the argv array, or in fact allocate a new argv structure
and change argv to point to the new one.
Being able to change argv to point to a different array, allocated or
not, follows from the required passing of arguments by value -- unless
main is special -- so I would absolutely expect that. Beyond that, I
don't see why it should be more or less reasonable to change the
pointers in argv versus the string contents they point to. In
practice, on every implementation I know of, both of those are in fact
modifiable, and I can't come up with a plausible way they wouldn't,
but the Standard guarantees only the latter not the former.

The only rationale I have to hand, n937, says nothing relevant. At a
wild guess, I suspect it might be because by the time of C89 some Unix
programs (arguably too many, but that's a different issue) did clobber
some or all of their arg string contents as a way to fiddle their
display by 'ps', but that didn't require changing the string pointers
and AFAIK none did.
On rereading the Standard (from 5.1.2.2.1 Program startup)

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.

I can imagine that that means my alternative interpretation without too much stretch.

It might mean not that the content of the original strings are modifiable, but
that the strings themselves can be substituted for other strings.
I can't (imagine that). It says "the strings pointed to by the argv
array", although to be absolutely clear it probably should say "by the
elements of the array the first element of which argv points to" or
simpler just repeat the earlier usage "by argv[0] to argv[argc-1]",
and "string" is defined in 7.1.1 as "a contiguous sequence of
characters terminated by and including the first null character.".
It isn't explicitly stated, but is clear throughout, this sequence is
actually (in) an array of char. Or alternatively, is in an object,
all of whose bytes are char values and thus can be accessed as array
of char. Given those, I don't think you can read 5.1.2.2.1 as
referring to the string pointers rather than their contents.

In particular, C programmers often talk loosely about a (qualified)
char* as "being" a string, but AFAIHS the Standard never does, it
consistently says "points to" or conversely "pointed to by".
Where are the examples? The Standard should be overflowing with examples
of code that reinforce what the prose text says.
554 pages isn't enough for you? You want 2,000? How many examples
would it take to fully cover all possibilities just for valid and
invalid usage of main's arguments? 5? 10? 20? 100?

And when mistakes are made and the examples disagree -- as some surely
will -- you won't be able to trust them anyway. I think there's too
many examples already; it makes it harder to search for some terms.
(But I'm not asking for anyone to go to the work of removing them, at
least not unless they are found to be defective.)

- David.Thompson1 at worldnet.att.net
 
L

Larry__Weiss

Dave said:
554 pages isn't enough for you? You want 2,000? How many examples
would it take to fully cover all possibilities just for valid and
invalid usage of main's arguments? 5? 10? 20? 100?

And when mistakes are made and the examples disagree -- as some surely
will -- you won't be able to trust them anyway. I think there's too
many examples already; it makes it harder to search for some terms.
(But I'm not asking for anyone to go to the work of removing them, at
least not unless they are found to be defective.)

Incorrect examples can be corrected, and the process of correcting them
itself would be a illuminating event lending understanding to the correct
interpretation of the text of the Standard.

Correctly constructed examples can only serve to reinforce the text
of the Standard. Perhaps they need their own section of the Standard.
I too find that the examples that exist in the midst of the Standard text
(primarily as footnotes) are not always the best use of the typographical
space within the Standard document itself.

I don't want the examples to be normative, but I do want the examples
to be refereed by the same process as any other text of the Standard.
 
L

Larry__Weiss

Richard said:
On the enlarging-strings side, neither have I. In fact, I don't believe
there's a guaranteed-safe way to do this. I think you're okay with argv[0]
though.

An amazing amount of uncertainty in such a simple matter!

- Larry Weiss
 
J

Joe Wright

Larry__Weiss said:
Richard said:
On the enlarging-strings side, neither have I. In fact, I don't believe
there's a guaranteed-safe way to do this. I think you're okay with argv[0]
though.

An amazing amount of uncertainty in such a simple matter!
Simple indeed. argv points to an array of argc+1 pointers to char. Each
of these pointers points to a string of length 0 or more. All is
apparently technically modifiable. The limits of this freedom is not
explained.

One might modify a string by changing certain characters and/or
shortening it. How one might 'add' to it is not described. I'm sure the
array 'char *argv[]' of pointers to char is also modifiable. But what
would you point to?

Such an amazingly simple matter. Not!
 
L

Larry__Weiss

Joe said:
Larry__Weiss said:
Richard said:
On the enlarging-strings side, neither have I. In fact, I don't believe
there's a guaranteed-safe way to do this. I think you're okay with argv[0]
though.

An amazing amount of uncertainty in such a simple matter!
Simple indeed. argv points to an array of argc+1 pointers to char. Each
of these pointers points to a string of length 0 or more. All is
apparently technically modifiable. The limits of this freedom is not
explained.

One might modify a string by changing certain characters and/or
shortening it. How one might 'add' to it is not described. I'm sure the
array 'char *argv[]' of pointers to char is also modifiable. But what
would you point to?

Such an amazingly simple matter. Not!

I'm still not convinced by Richard H.'s reasoning that the Standard is
placing restrictions on the ability to modify the argv structure.

I think it is equally likely that the Standard is just stating some obvious
facts when it states:

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.

There are other obvious facts that it could have listed. Richard finds
significance to that omitted list. I don't.

- Larry Weiss
 

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,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top