From: Ryan B. <rmb...@em...> - 1998-02-28 03:33:51
|
Joshua McMinn wrote: > the problem is that you want (*yadda)[i] because [] has higher > precident than * > > however.... But wait!...there's more. :) > > */ > > > } > > It'd be better to do: > > #include <stdio.h> > > void func (char *yadda); > > int main (void) > { > char *yack = "Hello World!\0"; By definition, the string initializer (*s="") will append a nul character for you, the '\0' at the end is redundant. > > func (yack); Main must return a value to the operating system. Add 'return 0;' here. > } > > void func(char *yadda) { > for(int i=0; yadda[i] != NULL; i++) There is a very distinct difference between NULL and a nul character. A nul character is '\0' and NULL is implementation defined (for instance, it could very easily be "(void *)0", thus breaking this program. Strings end in nul characters. Luckily for us, though, they both result in zero either way, so replace "yadda[i] != NULL" with "yadda[i]". > printf("%c",yadda[i]); I don't understand why people are so reliant on formatted output. If you're only printing one character, just use putc(). In fact, it's possible that printf() is just calling putc() (after taking the time to figure out just what %c means). > } I have the feeling that the author may have been using a double pointer for a reason (most likely to edit the string), so here's my rendition w/ the double indirection in tact. /* I wish Netscape's email client would auto-indent :) */ #include <stdio.h> void func(char **str) { char *c; for (c = *str; *c; c++) putc(*c, stdout); } int main(void) { char *str = "Hello World!"; func(&str); return 0; } Unfortunately this is all very off-topic and any further questions should be either sent by private e-mail (feel free to ask me) or to comp.lang.c. -Ryan |