ARGC/ARGV Strangeness - Code is working

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Wed Sep 21 04:03:49 UTC 2005


On Tue, Sep 20, 2005 at 11:45:50PM -0400, pking123-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org wrote:
> Below is the code that finally works. I have by now heavily modified 
> this program to, first of all, go forward in the list rather than 
> backward; as well as to accept a range of numbers rather than a list. 
> So, I christen this first effort "backascii.c" :-)
> 
> The new, properly-working program is completed and is the one called 
> "ascii.c". Thanks to all who made suggestions. Biggest thanks goes to 
> Peter Fletcher, who made valuable suggestions, especially regarding 
> debugging.
> 
> Paul King
> 
> ----------8<--------snip----------8<--------snip---------------
> 
> #include <stdio.h>
> #include <strings.h>
> 
> int main (int argc, char *argv[]) {
>    /* this only takes a number as an argument */
>    int num = 0;
>    int count;
>    char p1[5];
>    for (count = argc - 1; (count > 0); count--) { 
>       strcpy(p1, argv[count]);

Bad habit.  What happens if your argument is longer than 4 chars?  Try
using strncpy(3).

>       num = atoi(p1);
>       printf ("%d\t'%c'\n", num, num);
>    }
>    return 0;
> }

Of course, in shell script, it becomes
    ascii() 
    {
	for i; do
	    echo "$i	`tostring $i`"
	done
    }

-- 
William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
	   http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
	  http://freshmeat.net/projects/bashdiff/
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list