[comp.unix.questions] ????? HELP!!! what is wring with this code? ???????

ssa@mrsvr.UUCP (6607) (10/07/88)

	For the life of me I can't figure out what is wrong with the following
	code!!!!  It is supposed to parse words from a string in which words
	are delimited by blanks or tabs, and print one word a line.  Simple
	right? well not quite.

	when I compile it I get a message:

"test.c", line 7: warning: illegal combination of pointer and integer, op =

	this of course referes to the line with the while loop.  When I run it
	the prompt just comes back, (I am not prompted to put in my string)

	and the funny part is when I run the same program in dbxtool (SUN WS)
	IT RUNS FINE !!!!!!!!

____________________________________________________________________________
#include <strings.h>
#define NULL (char *) 0
main()
{
	char buf[81];
	char *tokptr,*strptr=buf;
	gets (buf);
	while((tokptr=strtok(strptr," \t")) != NULL ){
		printf ("%s\n",tokptr);
		strptr=NULL;
	}
}
----------------------------------------------------------------------------


	So, what is wrong???

	Thanks in advance...

=======================================================================

      /         /| |   |\ \   | |
     /__       /_| |   |_\ \  | |     ...att!uwmcsd1!mrsvr!ssa
       /      /  | |   |  \ \ | |        (414) 547-9429
      / o    /   | |__ |   \ \| |        (414) 521-6607 (work)

=======================================================================

ron@ron.rutgers.edu (Ron Natalie) (10/07/88)

The error is a warning.  "Strtok" is not declared anywhere and
hence is assumed to return integer.  You stuff it in a char
pointer.  The C compiler is trying to help you write portable
code.

-Ron

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (10/07/88)

Ron Natalie:
> The error is a warning.  "Strtok" is not declared anywhere and
> hence is assumed to return integer.  You stuff it in a char

SunOS doesn't declare it in <strings.h>, but DOES declare it in <string.h>
				  ^^				      ^

Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|uw-beaver}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

gwyn@smoke.ARPA (Doug Gwyn ) (10/07/88)

In article <456@mrsvr.UUCP> ssa@mrsvr.UUCP (6607) writes:
>"test.c", line 7: warning: illegal combination of pointer and integer, op =
>#include <strings.h>
>	while((tokptr=strtok(strptr," \t")) != NULL ){

<strings.h> doesn't declare strtok, so it is assumed to return int
which is not compatible with the char * being assigned into.
<strings.h> was invented by Berkeley without regard for existing
practice.  <string.h> is the official standard header for the
str* functions, and it does declare strtok.

ries@jupiter (Marc Ries) (10/07/88)

In article <456@mrsvr.UUCP> ssa@mrsvr.UUCP (6607) writes:
>
>	For the life of me I can't figure out what is wrong with the following
>	code!!!!  It is supposed to parse words from a string in which words
[...]
>"test.c", line 7: warning: illegal combination of pointer and integer, op =
>	IT RUNS FINE !!!!!!!!
>#include <strings.h>
char *strtok();
>#define NULL (char *) 0
>main()
>{
>	char buf[81];
>	char *tokptr,*strptr=buf;
>	gets (buf);
>	while((tokptr=strtok(strptr," \t")) != NULL ){
[...]

   Even though it works, lint and the compiler will be happy if
   you tell it that you know that strtok() is going to return
   a character pointer ("char *strtok();"), not an int. 

Marc Ries
TRW Defense Systems Group/HMI
UUCP: {sdcrdcf,decvax,ucbvax}!trwrb!spp2!ries@trwspp.UUCP
Arpanet: ries@trwrb.TRW.COM

burgett@galaxy.COM (Michael Burgett) (10/07/88)

In article <456@mrsvr.UUCP> ssa@mrsvr.UUCP (6607) writes:
>
>
>"test.c", line 7: warning: illegal combination of pointer and integer, op =
>
>	while((tokptr=strtok(strptr," \t")) != NULL ){
>
>	So, what is wrong???

in the copy of strings.h that we have here, strtok isn't declared, try
adding a

char *strtok();

just before main.  ;-)


Mike Burgett

adobe!burgett@decwrl.dec.com

jsdy@hadron.UUCP (Joseph S. D. Yao) (10/08/88)

In article <456@mrsvr.UUCP> ssa@mrsvr.UUCP (6607) writes:
>"test.c", line 7: warning: illegal combination of pointer and integer, op =
...
>	and the funny part is when I run the same program in dbxtool (SUN WS)
>	IT RUNS FINE !!!!!!!!
[L1]>#include <strings.h>
[L2]>#define NULL (char *) 0
...
[L7]>	while((tokptr=strtok(strptr," \t")) != NULL ){

You do not mention under what machine or C compiler you get the error
message.  I suspect that, if you look in /usr/include/strings.h on
that system, you'll find that strtok() is not declared, while it is on
your Sun.  Since it is not declared, the C compiler believes that it
is a function returning 'int'.  (This is an historical bug from before
'void' existed.)

Although I like using system include files very much, I tend to try to
declare external functions in the body of each new function that uses
them.  This is good insurance in case - as here - one isn't properly
declared, or - as happens - the return value of function xxxfieonyou
in your massive project changes, in which case a later 'lint' run will
reveal the inconsistency.

	Joe Yao		jsdy@hadron.COM (not yet domainised)
	hadron!jsdy@{uunet.UU.NET,dtix.ARPA,decuac.DEC.COM}
	arinc,att,avatar,blkcat,cos,decuac,dtix,\
	ecogong,empire,gong,grebyn,inco,insight, \!hadron!jsdy
	kcwc,lepton,netex,netxcom,phw5,rlgvax,	 /
	seismo,sms,smsdpg,sundc,uunet		/

vch@attibr.UUCP (Vincent C. Hatem) (10/08/88)

In article <456@mrsvr.UUCP>, ssa@mrsvr.UUCP (6607) writes:
> 
> 	For the life of me I can't figure out what is wrong with the following
> 	code!!!!  It is supposed to parse words from a string in which words
	:
> 	when I compile it I get a message:

> "test.c", line 7: warning: illegal combination of pointer and integer, op =

> 	this of course referes to the line with the while loop.  When I run it
> 	the prompt just comes back, (I am not prompted to put in my string)

Hmm... works fine on this 3B2...

-Vince

-- 
Vincent C. Hatem                            | att ---->\ (available from any
AT&T International                          | ulysses ->\ Action Central site)
International Operations Technical Support  | bellcore ->\   
1200 Mt Kemble Ave, Basking Ridge, NJ 07920 | ihnp4 ----->\__ !attibr!vch  

steve@micropen (Steven J. Owens) (10/13/88)

In article <456@mrsvr.UUCP>, ssa@mrsvr.UUCP (6607) writes:
> 
> 	For the life of me I can't figure out what is wrong with the following
> 	code!!!!  It is supposed to parse words from a string in which words
> 	are delimited by blanks or tabs, and print one word a line.  Simple
> 	right? well not quite.

	Well, let's take a look at the line:

> 	while((tokptr=strtok(strptr," \t")) != NULL ){



> 	So, what is wrong???

		First of all, as normal practice I try not to #define
	NULL, as many systems define this to be a char * anyways. If
	you'd like, you could make the line read:

	while((tokptr=strtok(strptr," \t")) != (char *)NULL ){

	As this is good, portable coding practice.

		This is kind of a small point, but is strtok() defined
	to be char * ? Yeah, I know. The manual says it is, but that doesn't
	necessarily mean it is. Where I work, it's been our practice
	to define these ourselves as well whenever possible. It is simply
	good, portable practice to make sure things are what we think they
	are.

		Your setting the variable strptr to buf, while appearing 
	harmless( and probably is ) would make me wince. It is simply
	unnecessary to do that, but that is my personal preference.


						SJO


-- 
                                            Steve Owens @ Micropen, Inc.
					    Pittsford, N.Y. 14534

...!{ames|harvard|rutgers|topaz|...}!rochester!ur-valhala!micropen!steve

rb@ist.CO.UK (Bob Bishop) (10/13/88)

> HELP!!! what is wring with this code? ???????
		  ^^^^^

Has someone been watching "Allo Allo"?