473,789 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Argument Processing


Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.
Nov 14 '05
23 2182
su******@yahoo. com wrote:
no problem with the declartion it is fine


Context would have been useful. The original post had:

int main(int argc, char argv[])

which *is* wrong - it's `char *argv[]` or `char **argv` (I prefer
the latter, myself, but the former means the same thing as a
function argument declaration).

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #11
At about the time of 11/4/2004 10:00 PM, Ravi Uday stated the following:
"Daniel Rudy"
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in message news:fo******** *********@newss vr14.news.prodi gy.com...
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}

What are you passing to main from command line ?


Just test stuff to see how it works.
You might try adding this before the first printf.

if ( argc < 3 )
{
printf ("Insufficie nt arguments to main\n");
return 0;
}
I did as you suggested, now it comes up with argc = 4 and then core dumps.

- Ravi
--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.


--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.
Nov 14 '05 #12
At about the time of 11/5/2004 12:28 AM, Mike Wahler stated the following:
"Daniel Rudy"
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in message news:fo******** *********@newss vr14.news.prodi gy.com...
Hello,

I'm trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

See below.

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])

int main(int argc, char *argv[])

/* or */

int main(int argc, char **argv)

{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)

for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}


-Mike


Thanks to everyone who replied. The code now looks like this:

strata:/home/dcrudy/c 1047 $$$ ->cat argtest.c
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i; /* generic counter */

if (argc < 3)
{
printf("error: need more arguments.\n");
return(0);
}
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
So now when I run it, instead of core dumping, I get the following output:

strata:/home/dcrudy/c 1046 $$$ ->./argtest arg1 arg2 arg3
argc = 4
argv[0] = ./argtest
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3

Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again.

--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.
Nov 14 '05 #13
Daniel Rudy wrote:
Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again.

argv is char *argv[] .A n array, of pointers to char.
The pointers actually points to the 1. element of a
nul terminated array of chars, but you can't tell from
that declaration though.

Think of it as an array of pointers to C strings.

Nov 14 '05 #14
"Daniel Rudy"
<i0************ *************** *******@n0o1p2a 3c4b5e6l7l8s9p0 a1m2.3n4e5t6>
wrote in message news:kG******** ***********@new ssvr21.news.pro digy.com...

Thanks to everyone who replied. The code now looks like this:

strata:/home/dcrudy/c 1047 $$$ ->cat argtest.c
/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i; /* generic counter */

if (argc < 3)
{
printf("error: need more arguments.\n");
return(0);
}
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
So now when I run it, instead of core dumping, I get the following output:

strata:/home/dcrudy/c 1046 $$$ ->./argtest arg1 arg2 arg3
argc = 4
argv[0] = ./argtest
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3

Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array,
Actually, it's not. Arrays cannot be passed to or returned
from functions.
but is it an array
of char?
No. It's a pointer (to an array of pointers (to char)).
This allows for arguments of varying lengths.
A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.


:-)

-Mike
Nov 14 '05 #15

"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:8Y******** ***********@new s2.e.nsc.no...
Daniel Rudy wrote:
Now there is something about this that I do not understand. What
exactly is the nature of argv? I know it's an array, but is it an array
of char? A number of people pointed out that the * was required.
AFAIK, * is a pointer reference.

Thanks again. argv is char *argv[] .A n array, of pointers to char.


No, 'argv' is *not* an array, it's a pointer.
The pointers actually points to the 1. element of a
nul terminated array of chars, but you can't tell from
that declaration though.

Think of it as an array of pointers to C strings.


No. It's a pointer to such an array.

-Mike
Nov 14 '05 #16
Jack Klein <ja*******@spam cop.net> writes:
Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.


Just a question - were parentheses EVER required on a 'return'
statement in C?
Nov 14 '05 #17
>Jack Klein <ja*******@spam cop.net> writes:
Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.

In article <news:kf******* ******@alumnus. caltech.edu>
Tim Rentsch <tx*@alumnus.ca ltech.edu> wrote:Just a question - were parentheses EVER required on a 'return'
statement in C?


It is quite possible.

Note that the syntax for "struct" once used parentheses rather than
braces, too:

struct ( int a; int b; );

As late as Version 6 Unix, the op= operators were spelled the other
way around, initializers for static-duration objects were written
without an "=" sign, and there were other oddities:

int x 5;

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

printf(2, "this goes to stderr: argc = %d\n", argc);

argc =- 1; /* ignore program name */
argv =+ 1;

...

}

The "standard I/O" library was originally a separate option; to
use it you had to link with "-lS" (I think -- I never actually
*used* V6, much less anything earlier like V5).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #18
Chris Torek <no****@torek.n et> writes:
Jack Klein <ja*******@spam cop.net> writes:
Note that 'return' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.


In article <news:kf******* ******@alumnus. caltech.edu>
Tim Rentsch <tx*@alumnus.ca ltech.edu> wrote:
Just a question - were parentheses EVER required on a 'return'
statement in C?


It is quite possible.

[numerous obsolete/obsolescent C-isms noted]


It might be good to ask the question this way - can any CLC-er
identify an old C compiler (or even remember using such a compiler)
where the syntax for 'return' required parentheses?

Nov 14 '05 #19
In article <9w************ *******@newssvr 21.news.prodigy .com>,
i0************* *************** ******@n0o1p2a3 c4b5e6l7l8s9p0a 1m2.3n4e5t6 says...
for (i = 0; i <= argc; i++)


What's wrong with this picture?

--
Randy Howard (2reply remove FOOBAR)
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1637
by: Olaf Meding | last post by:
What does the below PyChecker warning mean? More importantly, is there a way to suppress it? PyChecker warning: ..\src\phaseid\integration.py:21: self is argument in staticmethod My best guess is that the warning is related to PyChecker not supporting C++ extensions.
1
3650
by: Patrick Rammelt | last post by:
Hi After upgrading my compiler (gcc-3.3.3 to gcc-4.0.0) I stumbled over an error that I do not understand. Maybe it is a compiler bug, but maybe I can get some new insights from this. Here is a small example: ------------------------------------------------------------------ template <class T> class A { public:
10
2992
by: Richard Heathfield | last post by:
I was recently asked whether it is legal to start processing a variable argument list a second time, /before/ you've finished with it the first time. (I have no idea why the questioner might want to do this, I'm afraid. Since he's normally fairly bright, I presume he had a good reason for asking.) The following code illustrates the point, I think (but don't use it as a va_arg tutorial!, as it kinda assumes there are at least six args...
19
4261
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
6
8844
by: Sunny | last post by:
Hi, I have a very serious issue at hand. I have created a small C# Console App (Csd.exe) that collects a list of files as its argument and generates their Md5 sets. This application is used by other applications in my dept, which simply call this Csd.exe app and passing a list of files. I want to ask how can I return a value from this Csd.exe application as an indicator or a signal to the calling app that Csd.exe has finished working....
4
4651
by: MattBell | last post by:
I've tried to search for an answer to this without much success, and I think it's probably a common thing to do: I have a web service I want to accept an XmlDocument as an argument which conforms to a specific XSD that is defined. Right now when I declare XmlDocument as my argument, it puts the old xml:any type in. How do I change that to reflect the XSD that I'm looking for? Thanks for any Help!
3
3407
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
2
2177
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application. -------------------------------------------------------------------------------- Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/in configuration or <%@ Page EnableEventValidation="true"...
4
3296
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of arguments. For example, #define MAX_SIZE 100000 int foo(double *x, unsigned ling sz, int option) {
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.