LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2006, 08:08 PM   #1
debiant
Member
 
Registered: Jul 2006
Distribution: Source Mage 0.9.6
Posts: 196

Rep: Reputation: 30
comparison between pointer and integer a problem?


This snippet of code:

Code:
#include <stdio.h>
#include <stdlib.h>


int ftoc(int *x)
{
  int y;
  y = (*x - 32) * 5 / 9;
  return y;
}

int main(int argc, char *argv[])
{

   int a;
   a = atoi(argv[1]);
   if (a==NULL )
   {
     printf("ERROR: not a temperature.\n");
     return 1;   
   }
   if (a==999)
   {
     int a=0;
     while (a <= 100)
     {
       printf("Fahrenheit: %d Celsius: %d\n",a,ftoc(&a));
       a = a + 10;
     }
   }
   else
     printf("Farhenheit: %d Celsius: %d\n",a,ftoc(&a));
   return 0;
}
returns

warning: comparison between pointer and integer

Is this a problem or more appropriately bad form?
Basically I'm wanting to test to make sure that the argument is a number, is there a better way to do this?
 
Old 08-25-2006, 09:57 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
whats warning you is that you are comparing an int against a pointer (NULL). That doesnt have a lot of sense besides atoi doesnt not return NULL. Acording to the man page, atoi doesnt detect errors. So be careful with it, or use strtol

Hope this is useful.
Cheers!
 
Old 08-25-2006, 10:20 PM   #3
debiant
Member
 
Registered: Jul 2006
Distribution: Source Mage 0.9.6
Posts: 196

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by demon_vox
Hi,
whats warning you is that you are comparing an int against a pointer (NULL). That doesnt have a lot of sense besides atoi doesnt not return NULL. Acording to the man page, atoi doesnt detect errors. So be careful with it, or use strtol

Hope this is useful.
Cheers!
Well it works somehow that if a is not a number it displays the error, How would I go about using strtol in this instance, I tried and failed several times so I finally settled with atol, and actually the program has progressed a little bit from there. This will probably look extremely sloppy, any suggestions for cleaning things up:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double ftoc(double *x)
{
  double y;
  y = (*x - 32) * 5 / 9;
  return y;
}
double ctof(double *w)
{
  double z;
  z = *w * 1.8 + 32;
  return z;
}

int main(int argc, char *argv[])
{
   if (strcmp(argv[1], "--help") == 0)
   {
     printf("    -c : convert to celsius\n");
     printf("    -f : convert to fahrenheit\n");
     return 0;
   }
   if (strcmp(argv[1], "-c") != 0 &&
       strcmp(argv[1], "-f") != 0)
   {
     printf("Invalid argument: type --help for help\n");
     return 0;
   }
   int a;
   double d;  
   a = atoi(argv[2]);
   d = atof(argv[2]);
   if (a==NULL)
   {
     printf("Invalid argument: type --help for help\n");
     return 1;
   }
   if (strcmp(argv[1], "-c") == 0)
     printf("Farhenheit: %G Celsius: %G\n",d,ftoc(&d));
   else if (strcmp(argv[1], "-f") == 0)
     printf("Fahrenheit: %G Celsius: %G\n",ctof(&d),d);
return 0;
}
 
Old 08-26-2006, 09:05 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,194

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
a is an int.
NULL is a pointer

When you compare the two, you are comparing two different types. That is what the compiler warns you for. The warning is there to warn you about possible errors you might have made.

The fun thing is, when a=0, a==NULL would return true anyway, since (int)NULL = 0 indeed.

Apart from the fact the (a==NULL) is syntactically not quite correct (but could be acceptable in certain cases) the problem is that your assumption that atoi returning 0 at an error is not right.

atoi does not do error checking at all, and it does certainly not return NULL at an error. Not a pointer, but maybe the interger 0. This might just be implementation dependent so you program might not work when compiled on a different platform of a different [version of] compiler.

If you do "man strtol" on your bash command line you see the syntax for that command, and you also see that it defines a parameter which can return an error indication. It is a bit more complicated than atoi, but it makes your program fail-safe.

jlinkels
 
Old 08-26-2006, 08:45 PM   #5
debiant
Member
 
Registered: Jul 2006
Distribution: Source Mage 0.9.6
Posts: 196

Original Poster
Rep: Reputation: 30
I understood why I got the warning, but now I understand the reasoning behind it. I tried reading the man page for strtol, I even googled for examples, and I still can't figure out how to work it in to this program. I'm just now starting to grasp the concept of pointers. Maybe someone could help me with an implementation in this instance that would verify that the argument is infact a number?

It would be much appreciated.

I'm glad you told me about the NULL thing though, I guess I wasn't quite grasping that. Trying to learn C in my spare time, and I'm really struggling with some logic decisions. Did you think the rest of the code looked functionable though? Not insanely verbose or anything?

Last edited by debiant; 08-26-2006 at 08:47 PM.
 
Old 08-28-2006, 05:47 AM   #6
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
Quote:
Originally Posted by debiant
Maybe someone could help me with an implementation in this instance that would verify that the argument is infact a number?
You really need to get your concepts right about pointers. Unfortunately, it cannot be explained in a post like this. I recommend you to get some good book about pointers in C. Simply said, pointers are just like any basic data types; you just need to know how to use it.
 
Old 08-28-2006, 06:16 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
During the conversion strtol and strtoul go through the string till they find a string terminator or an invalid character. They then stop and the argument endptr contains the last digit that was processed. For a valid number, you expect '\0'.

Please note that the functions can work with decimal (base 10) and hexadecimal (base 16) 'numbers'.

You can pass different strings to the program below (digits, mixed digits and text) and see how it behaves.

Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
char *endptr;
unsigned int value;

    if(argc==2)
    {
        value=strtoul(argv[1],&endptr,10);
        if(*endptr!='\0')
        {
            printf("'%s' not a number (%c failed)\n",argv[1],*endptr);
        }
        else
        {
            printf("0x%X\n",value);
        }
    }

    return 0;
}
PS other functions to look at are isdigit() etc

Last edited by Wim Sturkenboom; 08-28-2006 at 06:19 AM.
 
Old 08-28-2006, 07:57 PM   #8
debiant
Member
 
Registered: Jul 2006
Distribution: Source Mage 0.9.6
Posts: 196

Original Poster
Rep: Reputation: 30
Thank you so much Wim. Trying to learn C on your own when you have little to no programming experience can be challenging, but I really feel that it is the one I want to start with. I know it's powerful, and I don't yet want to jump into the OOP of C++ and JAVA. I appreciate the help.

jineshkj - I just went out and got the C Programming Language to help me along, I was trying internet tutorials, and it just wasn't working, so hopefully it will help me along a bit better.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
32bit Integer and 64bit Pointer Hoju2468 Programming 0 07-20-2006 09:44 AM
Comparison between pointer and integer ---> WHY?? its_godzilla Programming 10 01-28-2005 09:40 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
ISO C++ forbids comparison between pointer and integer? pimaster Programming 1 11-06-2003 01:45 PM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration