Go Down

Topic: ISO C++ forbids comparison between pointer and integer (Read 6999 times) previous topic - next topic

arduino2013

Hi there,

I've got a problem with if-statements:

My problem: I want to compare values from an array to predefined values so that I can print a special message for every case. My code:

Code: [Select]
x=Str1[8], HEX;
    y=Str1[7], HEX;
    z=Str1[6], HEX;
    a=Str1[5], HEX;
   
    if(x=="5D" && y=="92" && z=='C6' && a=='42')
    {lcd.print("xxx");}


The values themselves are from an array that is filled earlier. When I try to compile my code it says:  "ISO C++ forbids comparison between pointer and integer" How can I realize that comparison?

Thanks in advance.
arduino2013

PaulS

Code: [Select]
x=Str1[8], HEX;
This is NOT doing what you think it is. Loose the , HEX crap.

Quote
My problem: I want to compare values from an array

It would be good to know what kind of array.

If the array is a char array, then no element in the array can hold "5D". An element might, depending on the array type, hold '5D'.

I suggest that you head over to http://snippets-r-us.com for help with your snippets.

arduino2013

Hi PaulS,

it's an int array.

Thanks for your help.

KeithRB

You are assigning with the value of HEX in all cases.
http://en.wikipedia.org/wiki/Comma_operator

Making up your own language is *never* a good idea.

KeithRB

An int can never be "5D". use 0x5D as a constant.

arduino2013

Quote
An int can never be "5D". use 0x5D as a constant.


For better understanding my problem here's my full code:

Code: [Select]
include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 9, 5, 1, 3, 0);
SoftwareSerial rfid(7, 8);
SoftwareSerial xbee(10, 9);

//Prototypes
void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);

//Global var
int flag = 0;
int Str1[11];
char x;
char y;
char z;
char a;

//INIT
void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("   Wilkommen!");
  xbee.begin(9600);
  rfid.begin(19200);
  delay(10);
  halt();

}

//MAIN
void loop()
{
  read_serial();
}

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
//Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }

  if(!rfid.available()) {lcd.setCursor(0,1); lcd.print("Chip auflegen");}
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    lcd.setCursor(0, 1);
    x=Str1[8];
    y=Str1[7];
    z=Str1[6];
    a=Str1[5];
   
    if(x=="5D" && y=="92" && z=='C6' && a=='42')
    {lcd.print("xxx");}
   
    //lcd.print(Str1[8], HEX);
    //lcd.print(Str1[7], HEX);
    //lcd.print(Str1[6], HEX);
    //lcd.print(Str1[5], HEX);
    //lcd.print("               ");
   
    delay(1000); //
    check_for_notag();
  }
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

Tom Carpenter

Code: [Select]
if(x==0x5D && y==0x92 && z==0xC6 && a==0x42)

P.S. Call your variables *sensible* names - single letter names are useless in describing what is going on.

P.S. Furthermore, x,y,z,a are all only used in one function, so why waste memory, make them local.
~Tom~

arduino2013

Unfortunately it only shows the Tag-ID only executes the following part:

Code: [Select]
lcd.print(Str1[8], HEX);
    lcd.print(Str1[7], HEX);
    lcd.print(Str1[6], HEX);
    lcd.print(Str1[5], HEX);
    lcd.print("               ");


Why is that and how to resolve it?

arduino2013

Thanks for your help, Tom. I replaced my if-statement by yours but it only shows the Tag-ID.

AWOL

Code: [Select]
An int can never be "5D".
But an "int" can be 'SD', interestingly.

Tom Carpenter

In which case one or all of the following are not true:
x==0x5D
y==0x92
z==0xC6
a==0x42


What exactly does this:
lcd.print(Str1[8], HEX);
lcd.print(Str1[7], HEX);
lcd.print(Str1[6], HEX);
lcd.print(Str1[5], HEX);

display on the LCD?
~Tom~

arduino2013


PaulS

Quote
What exactly does this:
lcd.print(Str1[8], HEX);
lcd.print(Str1[7], HEX);
lcd.print(Str1[6], HEX);
lcd.print(Str1[5], HEX);

display on the LCD?

was a rhetoric question. It was designed to make you THINK about what you are doing vs. what you are seeing. Str1[n] contains a value. You are printing that value to the LCD using a specific method with specific arguments.

You should experiment with different values for those arguments to see what affect that has on the output. For instance, use DEC, OCT, and BIN as the second argument. Then, for giggles, use 7 as the second argument.

Note that in all cases, WHAT is being printed has not changed. The result has though. Do you know why?

When you do, then you can understand why your questions in this whole thread make no sense.

arduino2013

I can see that the result was converted into the specified number system. But how does that affect my plan to compare it to a special value and print a special text?

PaulS

Quote
But how does that affect my plan to compare it to a special value and print a special text?

The VALUE never changed. The output representation did.
Code: [Select]
if(Str[8] == 0x5D)
{
   // Do something when Str[8] contains 0x5D
}

Go Up