about argc and argv

L

Lew Pitcher

hi all,

please tell me with example, how the *argv[] point to the the no of
strings.

Your question doesn't make much sense. You seem to be asking for an
example of how *argv[] points to the number of argv strings. Well, it
doesn't.

However, given a main() function prototype of
int main(int argc, char *argv[]);
then, upon initial entry into main(),
argc will have been set to the count of the number of strings that
argv will point to, and
argv will have been set to point to an array of pointers to the
individual strings

Thus,
argv[0] will point to the "program name" string, what ever that is,
argv[1] will point to the first argument string,
argv[2] will point to the second argument string,
and so on, with
argv[argc-1] pointing to the last argument string, and
argv[argc] pointing at a NULL pointer

So,
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 0)
printf("Failure: argc is negative, and that's not allowed\n");
else
{
if (argc == 0)
printf("Success: argc is zero, so there are no argv[] strings
passed to main()\n");
else
{
int arg;
for (arg = 0; arg < argc; ++arg)
if (argv[arg] != NULL)
printf("Success: argv[%d] points to \"%s
\"\n",arg,argv[arg]);
else
printf("Huh????: argv[%d] points to a NULL\n",arg);
}
if (argv[argc] == NULL)
printf("Success: the last argv[] points to NULL\n");
else
printf("Failure: the last argv[] points to something, not NULL
\n");
}
return EXIT_SUCCESS;
}

will enumerate the string parameters passed through argv

HTH
 
M

Malcolm McLean

vicky said:
hi all,

please tell me with example, how the *argv[] point to the the no of
strings.

Here's a simple program to echo arguements to the user.

#include <stdio.h>

int main(int argc, char **argv)
{
int i;

for(i=0;i<argc;i++)
printf("***%s***\n", argv);
return 0;

}

If you are asking how the argv array is set up, main() isn't the real entry
point to the program. There is some startup-code which queries the
command-line interface for the input, and sets up strings for main() to
read. However it is very platform-specific and normally you never need to
know about it.
 
J

John Gordon

In said:
please tell me with example, how the *argv[] point to the the no of
strings.

argv is an array of character strings.
argc indicates how many entries the array contains.

So, for example, if you wanted to display the first string, you could
do it like this:

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

(Remember, C arrays are indexed starting at zero.)
 
E

Eric Sosman

John Gordon wrote On 06/27/07 16:38,:
please tell me with example, how the *argv[] point to the the no of
strings.


argv is an array of character strings.

Almost: argv is an array of pointers, and those
pointers point to the beginnings of character strings.
 
W

Walter Roberson

Malcolm McLean said:
If you are asking how the argv array is set up, main() isn't the real entry
point to the program. There is some startup-code which queries the
command-line interface for the input, and sets up strings for main() to
read. However it is very platform-specific and normally you never need to
know about it.

That's one possible implementation, but not the only one. main()
can indeed be "the real entry point to the program", if the
operating system's method of invoking the program so cooperates.
But having main() as the "real entry point" complicates using
main recursively.

If main() is implemented as "the real entry point", there is also
a bit of complication if an exit handler is registered. It often
makes implementation sense for there to be something that main()
can [at an implementation level] "return to", that does the
exit handler work. But that's only a practical matter, not
how the DS9K will handle it between 3:14 and 3:17 tomorrow afternoon.
 
R

Richard Heathfield

Eric Sosman said:
John Gordon wrote On 06/27/07 16:38,:
In <[email protected]> vicky
please tell me with example, how the *argv[] point to the the no of
strings.


argv is an array of character strings.

Almost: argv is an array of pointers, and those
pointers point to the beginnings of character strings.

Almost: argv is a pointer to the first element in an array of pointers.
 
K

Keith Thompson

Eric Sosman said:
John Gordon wrote On 06/27/07 16:38,:
In said:
please tell me with example, how the *argv[] point to the the no of
strings.


argv is an array of character strings.

Almost: argv is an array of pointers, and those
pointers point to the beginnings of character strings.

Almost. argv is a pointer to pointer to char. It points to the first
element of an array of pointers to char. Each element of that array
either is a null pointer, or points to a string (i.e., points to the
first character of a string).

argv can be declared as "char *argv[]", but that really means "char
**argv" (a rule that applies only to parameter declarations).
 
E

Eric Sosman

Richard said:
Eric Sosman said:
John Gordon wrote On 06/27/07 16:38,:
In <[email protected]> vicky


please tell me with example, how the *argv[] point to the the no of
strings.

argv is an array of character strings.
Almost: argv is an array of pointers, and those
pointers point to the beginnings of character strings.

Almost: argv is a pointer to the first element in an array of pointers.

Y'know, I thought to myself as I wrote it, "Self," I
thought, "some pedant is going to pedantize you for this.
How very pedantestrian."

As a function argument, an array is all but indistinguishable
from a pointer. For an explication of "all but," see the FAQ or
see http://math.boisestate.edu/GaS/patience/webop/pat16d.html .
 
B

Barry Schwarz

hi all,

please tell me with example, how the *argv[] point to the the no of
strings.

Your question doesn't make much sense. You seem to be asking for an
example of how *argv[] points to the number of argv strings. Well, it
doesn't.

However, given a main() function prototype of
int main(int argc, char *argv[]);
then, upon initial entry into main(),
argc will have been set to the count of the number of strings that
argv will point to, and
argv will have been set to point to an array of pointers to the
individual strings

Thus,
argv[0] will point to the "program name" string, what ever that is,
argv[1] will point to the first argument string,
argv[2] will point to the second argument string,
and so on, with
argv[argc-1] pointing to the last argument string, and
argv[argc] pointing at a NULL pointer

Just a nit since your code handles it correctly. argv[argc] doesn't
point to a NULL pointer; it is a NULL pointer. Or, it you prefer, it
is a pointer that has been set to NULL.



Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
Just a nit since your code handles it correctly. argv[argc] doesn't
point to a NULL pointer; it is a NULL pointer. Or, it you prefer, it
is a pointer that has been set to NULL.

Meta-nit: it's a null pointer, not a NULL pointer.
 
R

Richard Heathfield

Eric Sosman said:
Y'know, I thought to myself as I wrote it, "Self," I
thought, "some pedant is going to pedantize you for this.
How very pedantestrian."

Y'know, you were right.
As a function argument, an array is all but indistinguishable
from a pointer.

Very true, but we are discussing argv, which is not an argument but a
parameter. As function parameters, arrays and pointers are trivial to
distinguish. Basically, if it's a parameter, it can't be an array.
Easy.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top