Re: Using argv[1] in C program

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Bart Garst
Date:  
To: PLUG Discuss
Subject: Re: Using argv[1] in C program
On Sat, 2005-01-15 at 11:41, Bart Garst wrote:
> On Sat, 2005-01-15 at 10:19, David Koopman wrote:
> > I have a simple C question, hoping someone can help.
> >
> > I was doing this on one machine:
> >
> > let i=0; while [ $i -lt 1 ]; do
> > # some action
> > usleep 200000
> > done
> >
> > I then moved to a different machine and "usleep" was not a binary on the
> > new machine. So, I created a quick c file:
> >
> > shell> vi usleep200000.c
> >
> > int main() {
> > usleep(200000);
> > }
> >
> >
> > shell> make usleep200000
> >
> > Now I can accomplish this same thing:
> >
> > let i=0; while [ $i -lt 1 ]; do
> > # some action
> > ./usleep200000
> > done
> >
> >
> > So, now for my question....
> >
> > How do I change my C program to take the milliseconds to sleep from the
> > command line? I think it's something like this:
> >
> > int main(argc, argv) {
> > usleep(argv[1]);
> > }
> >
> > But that doesn't work, it says:
> >
> > cc     usleep.c   -o usleep
> > usleep.c: In function `main':
> > usleep.c:2: error: subscripted value is neither array nor pointer
> > make: *** [usleep] Error 1

> >
> >
> > help?
>
> I think what you're looking for is atoi(char*). (see `man atoi` for
> other options).
>
> The parameter to usleep needs to be an unsigned long.
>
> Bart


Sorry, I missed these things earlier.

Are you including the proper header files? You error message indicates
the error occurs on line 2 (that indicates you aren't).

I also noticed no types for argc & argv.

Contact me off-list if you want/need more help.

Bart

---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss