return char data types

Hi there, I am planning to receive data from another node and I assigned the variable to be like this :

const char* sr = " ";
const char* dr = " ";

this is how I put to receive the data :

 strcpy (sr,strtokIndx);
 strcpy (dr,strtokIndx);

but somehow it showed the weird symbol "&" instead of data I wanted to get, which is "GT".

at the sender side, I am assigning the "GT" like

const char *src_ID = "GT";

I think is something wrong with the data types.Can anyone help on this?

You need to provide your entire sketch if you want people to help. Your sr buffer is only 1 char in length but you are trying to put 2 chars into it? Buffer overrun....

noted. So I should put two spacing for the sr and dr?

Updated:
But still gt the same error

As suggested, please post your complete sketch

It was abit hard for me to upload all.But here is the related part:

const char* src_ID = "CF";
const char* sID = "   ";
const char* dID = "   ";

 if (rf95.recv(buf, &len))
    {
      RH_RF95::printBuffer("Received: ", buf, len);
      Serial.print("Got: ");
      Serial.println((char*)buf);
      strcpy (tempChars,buf);
      char * strtokIndx;
      strtokIndx = strtok (tempChars,",");
      strcpy (messageFromPC,strtokIndx);

      strtokIndx = strtok (NULL,",");   
      strcpy (sID,strtokIndx);       >> this should be getting the alphabet "GT"
  

      strtokIndx = strtok (NULL,",");
      strcpy (dID,strtokIndx);  >> this should be getting the alpahbet "CF"


if (dID == "CF")
      {
        char data [25];
        sprintf(data,"P,%s,%s,Yes",src_ID,sID);
        rf95.send(data,strlen(data)+1);
        rf95.waitPacketSent();
        Serial.println(data);                      
        Serial.println("This node is destination node");   
                
       }

but I am now getting "&" , instead of GT and CF....

if (dID == "CF") You can’t compare strings like that.

Try this:

byte  y = strcmp(diD, "CF");
if(y ==0)   //null-characters terminated strings have matched
{
     //do something
}

ok thanks.This is another problem I am facing actually!!
But still I am getting "&" as output instead of "GT" &"CF"
The "GT" should have no connection with the issues you all have pointed out as above.
Thanks for figuring out the another prob!

Try putting in some extra serial.PrintLn so you can see what happens on each step:

      strcpy (tempChars,buf);
      char * strtokIndx;
      strtokIndx = strtok (tempChars,",");
        serial.PrintLn (strtokIndx);
      strcpy (messageFromPC,strtokIndx);
        serial.PrintLn (messageFromPC);
      strtokIndx = strtok (NULL,",");
      strcpy (sID,strtokIndx);       >> this should be getting the alphabet "GT"
        serial.PrintLn (strtokIndx);
      serial.printLn (sID);

      strtokIndx = strtok (NULL,",");
        serial.PrintLn (strtokIndx);
      strcpy (dID,strtokIndx);  >> this should be getting the alpahbet "CF"
        serial.PrintLn (dID);

You should then see why you don't have the data you expect. If you need help with the output, put up the whole sequence of PrintLn output starting with buf (that you already print out).

I have tried to print out. It showed like this:

P >> this one no prob
& >> this should be GT
? >> the question mark is reversed in the serial monitor and it should be CF

Try putting in some extra serial.PrintLn so you can see what happens on each step:

Serial.println();

stephanie9:
I have tried to print out. It showed like this:

P >> this one no prob
& >> this should be GT
? >> the question mark is reversed in the serial monitor and it should be CF

The reason for putting the lines in as I showed in the code is so that you can see at each step what has happened, rather than just look at the end and see that isn’t what was expected.

Also for those of us not familiar with exactly what you are sending the contents on buf would help us.

Owh I see. I actually did put the serial println to show each of the output but I didn't paste it here.
Forgive my carelessness to mention that.

But there is basically the data I am getting,in which the "&" and "?'" are unwanted

So can you see at which line it goes wrong? (We can't since we can't see the data!)

Why are you defining the strings that way?

const char* sID = "   ";
.
.
.
strcpy (sID,strtokIndx);

Makes no sense to declare a pointer to a 'const char' and then try to modify the data at that pointer. You should have gotten at least a compiler warning for that.

Instead use:

char sID[3];

Now GT and CF are shown in the receiver side!!
But the matching part is still a problem.

byte  y = strcmp(diD, "CF");[color=#222222][/color]
if(y ==0)   //null-characters terminated strings have matched[color=#222222][/color]
{[color=#222222][/color]
     //do something[color=#222222][/color]
}

so for this one, If they are matching, should I put inside if or else?
not so familiar with this strcmp

Updated:
I put inside if and it works!!
now is another prob:

byte  y = strcmp(dID, "CF");
      if(y ==0)   //null-characters terminated strings have matched
       {
          char data [25];
          sprintf(data,"P,%s,%c,Yes,route: ",src_ID,sID,src_ID);

for the route, I am planning to append on it every time the message has been sent.
So it is going to be longer for more time of transmission.
How should I do for that?

What is being put into "data" when using %c and %s?
%c is for a single character, %s is for a string (character array).

countrypaul:
What is being put into "data" when using %c and %s?
%c is for a single character, %s is for a string (character array).

It works when I put %s for both of that. Thanks!!

For another prob as mentioned above :

byte  y = strcmp(dID, "CF");
      if(y ==0)   //null-characters terminated strings have matched
       {
          char data [25];
          sprintf(data,"P,%s,%s,Yes,route: %s ",src_ID,sID,src_ID);

What I mean is that, I wanted to add more data like "CF" for example append on the route part.
So that if the message has been transferred twice, it should show two ID there, by using append method

Sorry I don't quite follow what the problem is, what append method are you referring to?.
This might be caused by me being tired it being nearly midnight here and I was up early.