By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

about argc and argv

P: n/a
hi all,

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

Jun 27 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Jun 27, 3:16 pm, vicky <goelvi...@gmail.comwrote:
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
--
Lew

Jun 27 '07 #2

P: n/a

"vicky" <go*******@gmail.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
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[i]);
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '07 #3

P: n/a
In <11**********************@z28g2000prd.googlegroups .comvicky <go*******@gmail.comwrites:
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.)

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Jun 27 '07 #4

P: n/a
John Gordon wrote On 06/27/07 16:38,:
In <11**********************@z28g2000prd.googlegroups .comvicky <go*******@gmail.comwrites:

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

--
Er*********@sun.com
Jun 27 '07 #5

P: n/a
In article <b4******************************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:
>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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jun 27 '07 #6

P: n/a
Eric Sosman said:
John Gordon wrote On 06/27/07 16:38,:
>In <11**********************@z28g2000prd.googlegroups .comvicky
<go*******@gmail.comwrites:

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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '07 #7

P: n/a
Eric Sosman <Er*********@sun.comwrites:
John Gordon wrote On 06/27/07 16:38,:
>In <11**********************@z28g2000prd.googlegroups .comvicky
<go*******@gmail.comwrites:
>>>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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '07 #8

P: n/a
Richard Heathfield wrote:
Eric Sosman said:
>John Gordon wrote On 06/27/07 16:38,:
>>In <11**********************@z28g2000prd.googlegroups .comvicky
<go*******@gmail.comwrites:
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/patie...op/pat16d.html .

--
Eric Sosman
es*****@acm-dot-org.invalid

Jun 28 '07 #9

P: n/a
On Wed, 27 Jun 2007 12:36:03 -0700, Lew Pitcher
<lp******@teksavvy.comwrote:
>On Jun 27, 3:16 pm, vicky <goelvi...@gmail.comwrote:
>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
Jun 28 '07 #10

P: n/a
Barry Schwarz <sc******@doezl.netwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 28 '07 #11

P: n/a
Eric Sosman said:
Richard Heathfield wrote:
>Eric Sosman said:
>>John Gordon wrote On 06/27/07 16:38,:

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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 28 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.