LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   comparison between pointer and integer a problem? (https://www.linuxquestions.org/questions/programming-9/comparison-between-pointer-and-integer-a-problem-477424/)

debiant 08-25-2006 08:08 PM

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?

demon_vox 08-25-2006 09:57 PM

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!

debiant 08-25-2006 10:20 PM

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;
}


jlinkels 08-26-2006 09:05 AM

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

debiant 08-26-2006 08:45 PM

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?

jineshkj 08-28-2006 05:47 AM

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.

Wim Sturkenboom 08-28-2006 06:16 AM

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

debiant 08-28-2006 07:57 PM

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.


All times are GMT -5. The time now is 08:09 AM.