walton@ametek.UUCP (Steve Walton) (12/04/86)
Bill Mayhew says (and others commiserate): > I just ran into a minor annoyance with the Lattice C compiler >that we have here at work. If memory stikes me right, it's version >3.02. > >I discovered that when I read argv[n][n] that the string isn't always >terminated with a \0 as you'd expect. This seems to happen mostly (if >not only) when the string contains ascii characters that are digits. Ahem...argv is not a two dimensional array, but rather an array of pointers. As such, it CANNOT be addressed with two subscripts as argv[n][n]. There is a difference, see K&R for details. The correct way to find the \0 at the end of the j'th argument is: char *s; s = argv[j]; for (i = 0; s[i] != '\0'; i++); /* i is now length of arg j */ If I badly misunderstood Bill's posting and have therefore insulted everyone's intelligence, forgive me. Stephen Walton ARPA: ametek!walton@csvax.caltech.edu Ametek Computer Research Div. BITNET: walton@caltech 610 N. Santa Anita Ave. UUCP: ...!ucbvax!sun!megatest!ametek!walton Arcadia, CA 91006 USA 818-445-6811
jec@iuvax.indiana.EDU (12/06/86)
That is not quite right, argv is indeed an array of pointers to strings, but strings can be arrays of characters. For instance: ------ #include <stdio.h> main(argc, argv) int argc; char *argv[]; { char c; int i; if (argc != 2) { fprintf(stderr, "usage: %s <some string>\n", argv[0]); exit(1); } printf("(1) argv[1] == %s\n", argv[1]); printf("(2) argv[1] == "); for (i=0;;i++) { c = argv[1][i]; if (c == '\0') break; putchar(c); } putchar('\n'); } ----- should produce the same results on any intellegent compiler. III Usenet: iuvax!jec UUU I UUU jec@indiana.csnet U I U U I U Phone: (812) 335-5561 U I U U.S. Mail: Indiana University UUUIUUU Dept. of Computer Science I 021-C Lindley Hall III Bloomington, IN. 47405
rmariani@watmum.UUCP (Rico Mariani) (12/06/86)
In article <816@ulowell.UUCP> walton@ametek.UUCP (Steve Walton) writes: >Ahem...argv is not a two dimensional array, but rather an array of pointers. >As such, it CANNOT be addressed with two subscripts as argv[n][n]. There >is a difference, see K&R for details. The correct way to find the \0 at >the end of the j'th argument is: > char *s; > s = argv[j]; > for (i = 0; s[i] != '\0'; i++); /* i is now length of arg j */ >If I badly misunderstood Bill's posting and have therefore insulted >everyone's intelligence, forgive me. Bzzzt WRONG! The first part about argv not being a two dimensional array is correct (sorf of) but there is nothing wrong(!) with accessing it as argv[i][j] to the the j-th character of the i-th string, provide that i<argc and j is not off the end of the string in question. Mind you, if I were trying to find the \0 I would do it much the same way as Stephen does in his example just because its more efficient that two subscripts (no compiler optization cracks please) But the best way to find the zero is this: i = strlen(argv[j]); works every time :-) -Rico
jim@ism780c.UUCP (Jim Balter) (12/07/86)
In article <816@ulowell.UUCP> walton@ametek.UUCP (Steve Walton) writes: >Bill Mayhew says (and others commiserate): >> I just ran into a minor annoyance with the Lattice C compiler >>that we have here at work. If memory stikes me right, it's version >>3.02. >> >>I discovered that when I read argv[n][n] that the string isn't always >>terminated with a \0 as you'd expect. This seems to happen mostly (if >>not only) when the string contains ascii characters that are digits. > >Ahem...argv is not a two dimensional array, but rather an array of pointers. >As such, it CANNOT be addressed with two subscripts as argv[n][n]. There >is a difference, see K&R for details. The correct way to find the \0 at >the end of the j'th argument is: > char *s; > s = argv[j]; > for (i = 0; s[i] != '\0'; i++); /* i is now length of arg j */ >If I badly misunderstood Bill's posting and have therefore insulted >everyone's intelligence, forgive me. > >Stephen Walton ARPA: ametek!walton@csvax.caltech.edu >Ametek Computer Research Div. BITNET: walton@caltech >610 N. Santa Anita Ave. UUCP: ...!ucbvax!sun!megatest!ametek!walton >Arcadia, CA 91006 USA >818-445-6811 You are wrong. You did not read or did not understand your C reference manual, and definitely did not test your claim before stating it as fact. Please act more resonsibly in the future and avoid insulting your *own* intelligence. Given the parameter or extern declarations (for definitions, "[]" is illegal, and a specific dimension must be given; for declarations, the innermost dimension can be left unspecified) "char **foo;" or "char *foo[];" or "char foo[][10];", even though these are represented in memory in three different ways (only two for parameters; "char **foo;" and "char *foo[];" are the same in that case, since an array cannot be passed on the stack, so foo must be a pointer and not a location) the references "foo[n][m]", "*(foo[n] + m)", and "*(*(foo + n) + m)" are all valid and all produce the same results. See K&R for details indeed. Do not trust in false wizards. -- -- Jim Balter ({sdcrdcf!ism780c,ima}!jim)
hamilton@uiucuxc.cso.uiuc.edu (12/07/86)
walton@ametek says: > Ahem...argv is not a two dimensional array, but rather an array of pointers. > As such, it CANNOT be addressed with two subscripts as argv[n][n]. There > is a difference, see K&R for details. actually, argv isn't even an array of pointers; it's a pointer to an array of pointers. true, there ARE some differences; "sizeof(argv)" is independent of the size of the strings, for instance. but it is time-honored tradition to reference arg string characters by double-indexing argv. this is part of the beauty of C's pointer/array semantics. wayne hamilton U of Il and US Army Corps of Engineers CERL UUCP: {ihnp4,pur-ee,convex}!uiucdcs!uiucuxc!hamilton ARPA: hamilton%uiucuxc@a.cs.uiuc.edu USMail: Box 476, Urbana, IL 61801 CSNET: hamilton%uiucuxc@uiuc.csnet Phone: (217)333-8703 CIS: [73047,544] PLink: w hamilton