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. 23 2124
On Fri, 05 Nov 2004 05:41:31 GMT, Daniel Rudy
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in comp.lang.c: 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++)
Replace "<=" with "<" in the loop. argv[argv] is a null pointer.
{ printf("argv[%d] = %s\n", i, argv[i]); }
/* return to operating system */ return(0);
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.
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Daniel Rudy"
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in message news:fo*****************@newssvr14.news.prodigy.co m... 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 ?
You might try adding this before the first printf.
if ( argc < 3 )
{
printf ("Insufficient arguments to main\n");
return 0;
}
- 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 wrote: #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++)
Rewrite this as
for (i = 0; i < argc; i++)
{ printf("argv[%d] = %s\n", i, argv[i]); }
/* return to operating system */ return(0); }
Satyajit
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
I think the problem is the declareation of the main function,
should be :
int main(int argc, char* argv[])
also should modify the <= to <
Daniel Rudy wrote: 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?
[snip]
cat main.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[]) {
printf("argc = %d\n", argc);
for (int i = 0; i < argc; ++i) {
printf("argv[%d] = %s\n", i, argv[i]);
}
/* return to operating system */
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c ./main arg1 arg2 arg3
argc = 4
argv[0] = ./main
argv[1] = arg1
argv[2] = arg2
argv[3] = arg3 gcc --version
gcc (GCC) 3.4.2 uname -o
Darwin man 3 getopt
GETOPT(3) BSD Library Functions Manual GETOPT(3)
NAME
getopt - get option character from command line argument list
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <unistd.h>
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int
getopt(int argc, char * const *argv, const char *optstring);
On Fri, 05 Nov 2004 00:01:59 -0600, Jack Klein wrote: ... argv[argv] is a null pointer.
Of course you mean argv[argc] is a null pointer.
--Mac
"Daniel Rudy"
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in message news:fo*****************@newssvr14.news.prodigy.co m... 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
no problem with the declartion it is fine
<su******@yahoo.com> wrote in message
news:10********************@c13g2000cwb.googlegrou ps.com... no problem with the declartion it is fine
Look again. It's not.
-Mike 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
At about the time of 11/4/2004 10:00 PM, Ravi Uday stated the following: "Daniel Rudy" <i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6> wrote in message news:fo*****************@newssvr14.news.prodigy.co m...
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 ("Insufficient 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.
At about the time of 11/5/2004 12:28 AM, Mike Wahler stated the following: "Daniel Rudy" <i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6> wrote in message news:fo*****************@newssvr14.news.prodigy.co m...
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.
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.
"Daniel Rudy"
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in message news:kG*******************@newssvr21.news.prodigy. 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
"Nils O. Selåsdal" <NO*@Utel.no> wrote in message
news:8Y*******************@news2.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
Jack Klein <ja*******@spamcop.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?
>Jack Klein <ja*******@spamcop.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.caltech.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.
Chris Torek <no****@torek.net> writes: Jack Klein <ja*******@spamcop.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.caltech.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?
In article <9w*******************@newssvr21.news.prodigy.com> ,
i0**********************************@n0o1p2a3c4b5e 6l7l8s9p0a1m2.3n4e5t6 says... for (i = 0; i <= argc; i++)
What's wrong with this picture?
--
Randy Howard (2reply remove FOOBAR)
In <kf*************@alumnus.caltech.edu> Tim Rentsch <tx*@alumnus.caltech.edu> writes: Just a question - were parentheses EVER required on a 'return' statement in C? http://www.lysator.liu.se/c/bwk-tutor.html
What if we wanted count to return a value, say the number of
characters read? The return statement allows for this too:
int i, c, nchar;
nchar = 0;
...
while( (c=getchar( )) != '\0' ) {
if( c > size || c < 0 )
c = size;
buf[c]++;
nchar++;
}
return(nchar);
Any expression can appear within the parentheses.
This is a strong implication that, by the time this tutorial was written,
4 years before K&R1, returning a value *required* the parentheses.
Even K&R1 consistently uses them when returning values, although the
syntax specification in Appendix A doesn't require them. It must have
been a very recent change to the language syntax.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bU******************@newsread1.news.pas.eart hlink.net>... "Daniel Rudy" <i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6> wrote in message news:kG*******************@newssvr21.news.prodigy. 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.
Actually, it's just a pointer to pointer to char, not a pointer to an
array of pointers to char (otherwise it would have to be typed char
*(*argv)[SIZE]). A number of people pointed out that the * was required. AFAIK, * is a pointer reference.
Right. Logically speaking, you're passing an array of strings to
main(). Physically speaking, this translates as a pointer to a
pointer to char:
int main (int argc, char *argv[])
or
int main (int argc, char **argv)
Here's why. Remember that a "string" in C is a 0-terminated array of
char:
char a[6] = "Hello"; /* a == {'H', 'e', 'l', 'l', 'o', 0} */
Therefore, an array of strings would be an array of 0-terminated
arrays of char:
char b[2][6] = {"Hello", "World"};
Now, remember that when you pass an array as an argument to a
function, what actually happens is that you pass a *pointer* to the
first element of the array. In other words:
void foo(char *arr) {...}
....
foo(a); /* a == &a[0] */
foo(b[0]); /* b[0] == &b[0][0] */
foo(b[1]); /* b[1] == &b[1][0] */
....
Even though the actual *types* of a, b[0], and b[1] are "6-element
array of char", when an array identifier appears in any context other
than as an operand to sizeof, it is evaluated as a pointer to the
first element in the array.
Now, since b is itself an array of something, when we pass it to a
function, we are actually passing the pointer to the first element:
void bar(char **arr) {...}
....
bar(b); /* b == &b[0] */
....
In the context of a function prototype, a[] and *a are equivalent;
both indicate that a is a pointer to something. *b[] and **b are also
equivalent. Since this tends to cause no end of confusion, I stick
with the *a, **b syntax exclusively.
"Mike Wahler" <mk******@mkwahler.net> wrote: "Nils O. Selåsdal" <NO*@Utel.no> wrote: Daniel Rudy wrote:
What exactly is the nature of argv? I know it's an array,
argv is char *argv[] .A n array, of pointers to char.
No, 'argv' is *not* an array, it's a pointer.
Think of it as an array of pointers to C strings.
No. It's a pointer to such an array.
If we're going to this level of ped^H^H^H detail,
it's actually a pointer to the first item of such an array. Da*****@cern.ch (Dan Pop) writes: In <kf*************@alumnus.caltech.edu> Tim Rentsch <tx*@alumnus.caltech.edu> writes:
Just a question - were parentheses EVER required on a 'return' statement in C?
http://www.lysator.liu.se/c/bwk-tutor.html
What if we wanted count to return a value, say the number of characters read? The return statement allows for this too:
int i, c, nchar; nchar = 0; ... while( (c=getchar( )) != '\0' ) { if( c > size || c < 0 ) c = size; buf[c]++; nchar++; } return(nchar);
Any expression can appear within the parentheses.
This is a strong implication that, by the time this tutorial was written, 4 years before K&R1, returning a value *required* the parentheses.
Even K&R1 consistently uses them when returning values, although the syntax specification in Appendix A doesn't require them. It must have been a very recent change to the language syntax.
This combination of observations explains a lot. Thanks Dan. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |