Very elementary question concerning indirection
Shiping Zhang
ping at cubmol.bio.columbia.edu
Thu Feb 15 10:00:34 AEST 1990
In article <7998 at hubcap.clemson.edu> kaires at hubcap.clemson.edu (Robert G Kaires) writes:
>I am just starting to learn C and I have what I'm sure is a very simple
>question. Consider the following program fragment:
>main()
>{
> char *pointer;
> char string[300];
> gets(string);
> printf( "%c\n",*(strchr(string,40)) ); <---- line in question
> pointer = strchr(string,40);
> printf("%c\n",*pointer);
>}
By default, strchr() returns its value as an integer. So it is
illegal to use it as a pointer. To solve this problem, just
declare stachr() to be a function returning a pointer to char, as
following:
char *strchr();
By the way, it is not a good idea to use stachr() that way because
it returns zero if it does not find the character, which will cause
core dumping in this case. The returned value should be tested
before it is used.
-ping
More information about the Comp.lang.c
mailing list