Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

err msg:comparison between interger and pointer

Status
Not open for further replies.

cat5ive

MIS
Dec 3, 2004
184
US
Hi,

I copied this code from the book but it won't compile. the error msg as mention above. I'm using DEV-C++ v.4.9.9.0.

#include <stdio.h>

char input[81], *ptr;

int main()
{
puts("enter text a line at a time, then press enter");
puts("put a blank line when done");

// loop as ong as input is not a blank line

while (*(ptr = gets(input)) != NULL)
printf("you entered %s\n", input);

puts("thank you and good-bye");

system("PAUSE");
return 0;
}

Thanks for any help.
 
the err msg was "comparison between pointer and interger"

 
Are you sure it wasn't
Code:
#include <stdio.h>

char input[81], *ptr;

int main()
{
  puts("enter text a line at a time, then press enter");
  puts("put a blank line when done");
  
  // loop as ong as input is not a blank line
  
  while ([red](*[/red]ptr = gets(input)) != NULL)
    printf("you entered %s\n", input);
    
  puts("thank you and good-bye");
    
  system("PAUSE");    
  return 0;
}
in the book.

system("PAUSE");
...tries to call a program called PAUSE... if your on Unix or a Unix-like system you'll want to use the sleep command and pass it how long to sleep. If it complains about the system you may also need to include stdlib.h
 
Acording to the book it said, "By enclosing this expression in parentheses and preceding it with the indirection operator(*), the value stored at the pointed-to address is obtained."
Anyway, I tried it like this ((*ptr = gets(input)) != NULL).
It gave me more error.

Thanks
 
By the way,
...tries to call a program called PAUSE...
What's the exact statment for this? And what's wrong with this statement
system("PAUSE")
? I'm don't know much about C.

thanks
 
Acording to the book it said, "By enclosing this expression in parentheses and preceding it with the indirection operator(*), the value stored at the pointed-to address is obtained."
Anyway, I tried it like this ((*ptr = gets(input)) != NULL).
It gave me more error.
[/code]
The start means derference, and gets(...) (after looking it up again) returns a char* (a pointer), so

Code:
while ((ptr = gets(intput)) != NULL)
   //while the pointer we get back is non null

or

Code:
while (*(ptr = gets(input)) != NULL)
  //while we didn't get back the null charactor

Both SHOULD work (compile) but you want the later, so you can try to force it by casting them both to the same type...
Code:
while((char)*(ptr = gets(input)) != (char)NULL)

By the way,
...tries to call a program called PAUSE...
What's the exact statment for this? And what's wrong with this statement
system("PAUSE")? I'm don't know much about C.
There isn't anything wrong with it, if PAUSE is a command on your system... read, if your programming in Widows/DOS. If your running Linux this won't do what you think it will.

system() executes a command on the shell (read, it runs another program), and if your not using DOS/Windows it won't work....

The pause command is used within a computer batch file and allows the computer to pause the currently running batch file until the user presses any key.

So if your working in Windows this will keep the window from just closing (if you start it by double clicking on it). If you are running it in a DOS window you opened when the program is done, it'll wait till you press a key to close the program.

The point I was making is that it just won't do anything but display a shell error message "Command not found" if you compiled and ran it under say Linux.
 
Oh, and the first responce I gave was a mistake on my part, just to clarify.
 
Yes, it works now (by forcing it as you suggested)

Thank you very much for the help and the explanation.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top