473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

about argc and argv

hi all,

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

Jun 27 '07 #1
11 6018
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

"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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mg | last post by:
Hello, I am writting bindings for a FEM application. In one of my function 'initModulename', called when the module is imported, I would like to get the argc and argv arguments used in the main...
9
by: Vinu | last post by:
Hai when i compile the cpp file(cmdargs.cpp) i attached the output below the program int main(int argc, wchar_t* argv) { std::wcout<<L"Name of the Program is "<<*argv<<std::endl;...
19
by: hpy_awad | last post by:
What should I do to avoid the message : parameter 'argc' is never used My program is down : *-------------------- /* Book name : The prodessional programmers guide to C File name ...
29
by: Merrill & Michele | last post by:
I'm now looking at page 115 K&R. After having discussed counterexamples, I shall herewith and henceforth make all my c programs look like int main(int orange, char* apple) {return(0);} Q1) ...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
4
by: hn.ft.pris | last post by:
####################################### ........ void argParser(int, char**); int main(int argc, char** argv){ argParser(argc, argv); return 1; } void argParser(int argc, char** argv){
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
4
by: Romulo Carneiro | last post by:
Hi, I'm programming in Windows XP and i'm trying to get all arguments of some application, but i only have gotten five argv. When i put more then five(5), it didn't display. =>Input Command Line:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.