kaires@hubcap.clemson.edu (Robert G Kaires) (02/15/90)
Hello,
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);
}
"partest.c", line 6: illegal indirection
"partest.c", line 7: warning: illegal combination of pointer and
integer, op =
If the "line in question" is commented out, the program compiles with one
warning on line 7 (This is in Vax Ultrex C, no warning is given in Turbo
C). What is wrong with the "line in question". Also, what does this
warning mean?
Thanks for all replies!
Bob Kaires
ping@cubmol.bio.columbia.edu (Shiping Zhang) (02/15/90)
In article <7998@hubcap.clemson.edu> kaires@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
zvs@bby.oz.au (Zev Sero) (02/15/90)
In article <7998@hubcap.clemson.edu> kaires@hubcap.clemson.edu (Robert G Kaires) writes: 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); } "partest.c", line 6: illegal indirection "partest.c", line 7: warning: illegal combination of pointer and integer, op = The first thing to do is to declare strchr as returning char *. #include <string.h> The second point to consider is what happens if there is no '(' in the input line. In this case, strchr will return NULL, which will crash when you dereference it. What you might want to do is : pointer = strchr (string, '('); if (pointer) printf ("%c\n", *pointer); I presume you want to do more with it than that, or you could simply putchar ('('); Also, be aware that gets() is dangerous. If you give it more than 299 characters, it will crash. Use fgets (string, 299, stdin); string[strlen (string) - 1] = '\0'; This will get you up to 298 characters, without the '\n' at the end. -- Zev Sero - zvs@bby.oz.au Megalomaniacs are simply people who know damn well they can run the universe better then God or the present governors. - Abner Doon (Orson S. Card)
rkl@cbnewsh.ATT.COM (kevin.laux) (02/16/90)
In article <7998@hubcap.clemson.edu>, kaires@hubcap.clemson.edu (Robert G Kaires) writes: | | 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); | } | | "partest.c", line 6: illegal indirection | "partest.c", line 7: warning: illegal combination of pointer and | integer, op = | Did you include the header file <string.h|? If not, then strchr() defaults to a function that returns an int, not a character pointer. --rkl
david@csource.oz.au (david nugent) (02/16/90)
> printf( "%c\n",*(strchr(string,40)) ); <---- line in question > > If the "line in question" is commented out, the program compiles with > one warning on line 7 (This is in Vax Ultrex C, no warning is given in > Turbo C). What is wrong with the "line in question". Also, what does this > warning mean? It means I _like_ that compiler! :-) Hmm, one clever enough to understand printf() style format strings is rather nice. It must know it's looking for a char there.. I'm not too surprised TC passes it with no comment. printf() is defined to have a variable number of arguments, and no type checking at all is in effect. david -- uucp: ...!munnari!csource!david internet: david@csource.oz.au Via FidoNet 3:632/348, Melbourne, Australia.
night@pawl.rpi.edu (Trip Martin) (02/16/90)
david@csource.oz.au (david nugent) writes: > > printf( "%c\n",*(strchr(string,40)) ); <---- line in question > > > > If the "line in question" is commented out, the program compiles with > > one warning on line 7 (This is in Vax Ultrex C, no warning is given in > > Turbo C). What is wrong with the "line in question". Also, what does this > > warning mean? >It means I _like_ that compiler! :-) >Hmm, one clever enough to understand printf() style format strings is rather >nice. It must know it's looking for a char there.. >I'm not too surprised TC passes it with no comment. printf() is defined to >have a variable number of arguments, and no type checking at all is in >effect. You're missing the problem here. The compiler is complaining about dereferencing an integer. Now you and I know that strchr() returns a pointer to a char, but in this case the compiler doesn't know that so it assumes that strchr() returns a pointer to a char, so it is correct in complaining about the dereference. The error message has nothing to do with the semantics of printf. -- Trip Martin night@pawl.rpi.edu