data converting/parsing help char* to char

Hi,

I'm currently working on using MQTT and Sparkfun's Wifly shield but am a bit stuck as to how to convert the char* a to a char array or compare the char array of 1 variable to another.

char* test1 = "arduino/test1";
void callback(char *a, uint8_t *b, int c) { 
  char buf[15];
  if(a == test1){
    Serial.println("test1!");
  }
  else{}

Above is the function I am having trouble with. With MQTT I am receiving a topic name(a) and a payload(b). For example if I publish topic name: "arduino/test" with the payload: "hello world", the output of the above function would be "arduino/test=hello world". However I can subscribe to mutliple topics but different topics contain different expecting data (one might contain numbers, on might contain floats, chars, bytes etc).

Look forward to any advice about my poorly explained problem :slight_smile:

Nick

What is your specific problem? Do you have code that isn't working the way you want it to, and if so, what is it doing versus what you want it to do?

I only ask because, for all intents and purposes, a char * and char array are the same thing. There is no conversion necessary. You can treat a char * as an array, or a char array as a char * (though it may not necessarily be appropriate to do so at all times).

Please revise your post, to put the code in code tags. It is impossible to read as is.

Sorry about that, I have updated the initial problem / code format. Basically I am trying to compare char* (a) with char* (test1).

Nick

 if(a = test1){

Couple problems with that line.

  1. It's performing an assignment, not a comparison. Comparisons are done using ==

  2. Even if it were doing a comparison, you are only comparing two pointers, not the value they are pointing to.

  3. Even if you were comparing the value they are pointing to, a simply comparison can only handle the first character they are pointing to.

If you want to compare two strings, you need to use the strcmp() function from the avr libc library. Well, you don't NEED to use that function, but you need to performance a character by character comparison of the two strings, which is what the function does. No point reinventing the wheel.

for (uint8_t i = 0; i < aLen; i++) { //here is the topic name
payload = a;
}

Means almost nothing !

it's like if you write (pseudo language)

for i=0 to 1000000000
payload = 10;
next

If you are using pointers or tabs things are not difficult :

for (i=0; i<strlen(a); i++)
payload_=a++;_
thats equivalent to :
int cpt=0;
while (*a)
_ payload[cpt++]=a
++;_
after, it ok :
payload[aLen++] = '=';
and then :
while(*b)
_ payload[aLen++]=*b++;_
You need to be more easy with pointers.
Pointers as the name describe them is like an arrow pointing to a case of memory (an address). It does nothing else than that.
this code means :

cpt = 0;
while(*a) <=> while the character pointed by a is different from 0
payload[cpt++]=*a++; <=> store dans payload index cpt the character pointed by a, increment the index, increment the pointer (means : go to the next case of memory).

Grag38 - did you notice the change in font in the original message? The [ i ] that was at the end of the payload = a; line got interpreted as an italic start tag. It's why I asked OP to use the code tags, so this doesn't happen.

OP - Try using the code button (#), not the quote button. Your code is still impossible to read without study and guessing. There is a reason that button is there...

Nicktriller:
...trying to compare char* (a) with char* (test1)

The standard library function strcmp(const char *s1, const char *s2) returns a value of zero if the two "strings" are equal:

    if (strcmp(a, test1) == 0) {
        // do whatever you do when the strings are identical.
    }
    else {
        // do whatever you do when the strings are not identical.
    }

Regards,

Dave

PaulS:
Grag38 - did you notice the change in font in the original message? The [ i ] that was at the end of the payload = a; line got interpreted as an italic start tag. It's why I asked OP to use the code tags, so this doesn't happen.

OP - Try using the code button (#), not the quote button. Your code is still impossible to read without study and guessing. There is a reason that button is there...

oops sorry gents !

All sorted now, thank you very much for the help. FYI @PaulS I did use the code button, it is clearly labelled for even the simple folk such as myself, however the issue was that I selected the "Copy for Forum" option in the arduino IDE hence the italic formatting - apologies.

Nick