char* argv[]

K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:

A pointer cannot be a string. [*]

I have been out-nitted. Yes, of course I mean that the program is treating
argv as a pointer to the first character in a string.

(And a pointer to the first character in a string may legitimately be
referred to as a pointer to the string -- which would not be the case
if "string" were a data type.)

But the program isn't actually doing that. Both versions had

printf("%s", argv[0]);

which treats argv[0], not argv, as a pointer to (the first character
of) a string. One version declared argv as ``char *argv'', but that
by itself doesn't imply that argv is a pointer to a string; it could
point to a single character, or to the first element of an array of
characters none of which are '\0'.
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield said:
Keith Thompson said:

A pointer cannot be a string. [*]

I have been out-nitted. Yes, of course I mean that the program is
treating argv as a pointer to the first character in a string.

(And a pointer to the first character in a string may legitimately be
referred to as a pointer to the string

I have never been very comfortable with that terminology...
-- which would not be the case
if "string" were a data type.)

....and that's why. Those people who - incorrectly but nevertheless quite
understandably - think of "string" as a synonym for "array of characters
terminated by the first null character" run the risk of mentally
translating "pointer to string" to "pointer to array", and suddenly
they're not only incorrect but in danger of confusing two legitimate
terminologies.
But the program isn't actually doing that. Both versions had

printf("%s", argv[0]);

Blargh! It gets worse? I didn't even notice the [0]! Possibly because it
looks so natural in context.
 
W

William Pursell

William Pursell said:



Your "correction" is incorrect.


No, in the second program argv is (incorrectly defined as) a pointer to the
*first element* in an array of characters.

A pointer to an array of characters is a pointer to
the first element.
The program then uses this
value as an argument matching a printf %s format specifier - in other
words, the program thinks argv is a string (just as I claimed), and is
using it as one.

What the program does with the argument is irrelevant. From
the declaration, the compiler determines that the
argument is a pointer to a character.
Non sequitur. I didn't claim that C has a string type.

You stated: "Your second program thinks argv is a string"
What does that mean? As far as I know, programs are
not capable of thought, so that sentence must mean
that the compiler interprets the declaration as
declaring that the argument is a string. It
follows that the author of the sentence, (you)
believe that the compiler has some concept of
what a string is. (Of course I realize you
don't think that C has a string type. But
I believe your sentence could be interpreted
that way, and should be clarified. Well,
actually, I was just giving you some good-natured
ribbing.)
 
R

Richard Heathfield

William Pursell said:

A pointer to an array of characters is a pointer to
the first element.

No, it isn't; they have different types, as this program will demonstrate.

#include <stdio.h>

int main(void)
{
char arr[13] = "Hello, world"; /* array of characters */
char (*parr)[13] = &arr; /* pointer to array of characters */
char *pch = arr; /* ptr to first element, eqvt to &arr[0] */
printf("%lu\n", (unsigned long)sizeof *parr);
printf("%lu\n", (unsigned long)sizeof *pch);
return 0;
}
 
F

Flash Gordon

Barry Schwarz wrote, On 19/12/07 04:19:
Why (I) works but (II) gives segmentation error?

(I)
int main(int argc, char *argv[]) {
printf("%s", argv[0]);
}

argv is an array of pointers. argv[0] is the first pointer in the
array.
(II)
int main(int argc, char *argv) {
printf("%s", argv[0]);
}

argv is a pointer to char. argv[0] is the char itself.

Well, it would be if char *argv was the correct type for the parameter.
However, the second parameter should have been "char **argv" or "char
*argv[]".
What type of parameter must you use to match a %s?

Trying to use argv would also be wrong, because the prototype for main
is wrong.
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top