getting wrong values?

I have this bit of code, and I'm getting some strange behavior out of it.

void codeCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  char param[NAMELEN];
  char value[VALUELEN];
  char *rawCode;
  int rawLen;
  
  server.httpSuccess();

  if (type == WebServer::POST)  {
    server.readPOSTparam(param, NAMELEN, value, VALUELEN);
    if(strcmp(param,"type") == 0) {
      if(strcmp(value,"raw") == 0) {
        server.readPOSTparam(param, NAMELEN, value, VALUELEN);
        if(strcmp(param,"code") == 0) {
          rawCode=value; //value = 4023,3432,4353
        }
        server.readPOSTparam(param, NAMELEN, value, VALUELEN); // This is where it goes wrong
        if(strcmp(param,"len") == 0) {
          rawLen = atoi(value); //value = 35
        }
      }
      Serial.println(rawCode); // outputs 35
      Serial.println(rawLen);  // outputs 35
      parseData(rawCode, rawLen);
    }
  }
}

As you can see from the comments. I'm trying to set rawCode to value, which at the time contains 4023,3432,4353. If I debug rawCode right after I do rawCode=value; it shows the proper 4023,3432,4353 ...But, when the code completes and I get to the two serial.println's rawCode is now 35, which is what rawLen is, and both rawCode and rawLen are set to 35. I want rawCode to have 4023,3432,4353 and rawLen to have 35. This is stumping me as to why rawCode is getting rawLen's value.

          rawCode=value; //value = 4023,3432,4353

Huh? rawCode is a pointer.

... but using it that way should be ok as it then becomes synonymous with value, which is a character array. I am just wondering is value[] is given the value 35 somewhere in the code. The print would be printing a character string (I assume) rather than the value of the pointer.

Er, yes but:

      rawCode=value; //value = 4023,3432,4353
...
      rawLen = atoi(value); //value = 35

      Serial.println(rawCode); // outputs 35
      Serial.println(rawLen);  // outputs 35

rawCode is a pointer pointing to the string in value.
value contains 35.
So, rawCode points to a string containing 35.

You have to distinguish between copying the pointer, and the thing it points to.

Correct me if I'm wrong, but what I think I got is I should be using a strcpy for rawCode to copy it rather than point to it?

If you want to make a copy of it, then yes, this should work:

strncpy(rawCode,value,VALUELEN);

Note that it uses strncpy, rather than strcpy just in case there is not a null character at the end of value[], or the null character is half way through it. This way it will always copy exactly VALUELEN chars into the new array.

TCWORLD:
If you want to make a copy of it, then yes, this should work:

strncpy(rawCode,value,VALUELEN);

Note that it uses strncpy, rather than strcpy just in case there is not a null character at the end of value[], or the null character is half way through it. This way it will always copy exactly VALUELEN chars into the new array.

I did the strncpy and I get weird output from it now.

4023,3432,43àÿ¸eÁ¸¸¸,¸¸¸¸¸¸¸¸¸aa¸¸¸0¸P¸ÿ¸¸¸¸¸¸R?¸¸¸?¸¸¸¸¸¸¸¸¸?¸¸?¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¦¸¸øþÿ¸¸B¸¸Ï¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpxÑ#
x&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0&O Jàà6? ?f!?6?%coden»â
?n¸¶0000qèε¦%³</ir00À¨kÀ¨ÒMeMÁ¸ÿIa Jàðï zà!?ð%Ô ,,Å%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpxñun&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0'O Jàà6? ?f!?6?%codeÿ050,4400,60àï¸eÁ¸¸¸,¸¸¸¸¸¸¸¸¸aa¸¸¸0¸P¸ÿ¸¸¸¸¸¸R?¸¸¸?¸¸¸¸¸¸¸¸¸?¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸ö¸¸øþÿ¸¸B¸¸Ï¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpxÇd&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0(O Jàà6? ?f!?6?%coden»â
?n¸¶0000qèε¦%³</ir00À¨kÀ¨ÒMeMÁ¸ÿIa Jàðï zà!?ð%Ô ,,Å%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpx1%&Z&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0)O Jàà6? ?f!?6?%codeÿ050,4400,60 à߸e  Á¸¸¸,¸¸¸¸¸¸¸¸¸aa¸¸¸0¸P¸ÿ¸¸¸¸¸¸R?¸¸¸?¸¸¸¸¸¸¸¸¸?¸¸a¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸øþÿ¸¸B¸¸Ï¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpxQ-k.P&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0*&O Jàà6? ?f!?6?%coden»â
?n¸¶0000qèε¦%³</ir00À¨kÀ¨ÒMeMÁ¸ÿIa Jàðï zà!?ð%Ô ,,Å%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpxq5½6F&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0&.O Jàà6? ?f!?6?%codeÿ050,4400,600àϸe00Á¸¸¸,¸¸¸¸¸¸¸¸¸aa¸¸¸0¸P¸ÿ¸¸¸¸¸¸R?¸¸¸?¸¸¸¸¸¸¸¸¸?¸¸±¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸h¸¸øþÿ¸¸B¸¸Ï¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸¸%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?óåïf(©?$?CÒÚë1 ¨Ù¹¸a-PèÎZ
-tnetnoC``ë¬
kkkPPkÀ¨kkkk@HPX`hpx?=?<&)[èX?ÅÄÀÁÆak00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FÏð5az5eae???>eÑMeìß=ô
co34a?0)6O Jàà6? ?f!?6?%coden»â
?n¸¶0000qèε¦%³</ir00À¨kÀ¨ÒMeMÁ¸ÿIa Jàðï zà!?ð%Ô ,,Å%31[^,]%nContent-Length:Authorization:

GET HEAD POST PUT DELETE PATCH Basic 
radiocheckboxtext/html; charset=utf-8typerawcodelentext/plainimage/x-iconCache-Control: max-age=31536000
/robots.txt/favicon.icoirBegin...À¨ÒÞ­¾ïþíÁ½r	ôÊ-<²ï?dD,±?

This slows the serial output to a crawl, I had to pull the usb and wait a bit to even be able to copy that. all I did was change rawCode=value; to strncpy(rawCode,value,VALUELEN);

My bad sorry, I misunderstood what VALUELEN was meaning. Try this:

strncpy(rawCode,"",15); //This will create a string of 15 null characters
strncpy(rawCode,value,14); //This will then replace the first 14 with the string you are interested in, leaving behind a null at the end.

It just seems to hang the arduino.

strncpy(rawCode,"",15);
strncpy(rawCode,value,14);

I put a serial debug right above it and the debug is the last thing before it just stops running and needs a reset.

Last I can think of to try is to change:

char* rawCode;
to:
char rawCode[15];

TCWORLD:
Last I can think of to try is to change:

char* rawCode;
to:
char rawCode[15];

I can't thank you enough. Seems to work now.

That's just a quick stop gap, and will only work as long as the value is short enough.

Now I know it works, this would be a more complete method:

void codeCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  char param[NAMELEN];
  char value[VALUELEN];
  char *rawCode;
  int rawLen;
  
  server.httpSuccess();

  if (type == WebServer::POST)  {
    server.readPOSTparam(param, NAMELEN, value, VALUELEN);
    if(strcmp(param,"type") == 0) {
      if(strcmp(value,"raw") == 0) {
        server.readPOSTparam(param, NAMELEN, value, VALUELEN);
        if(strcmp(param,"code") == 0) {
          char temp[strlen(value)+1]; //allocate the reqired amount of memory space for the code to be stored
          strcpy(temp,value);  //Copy the code into that space
          rawCode=temp; //The rawCode pointer can now point to the new array space, rather than the old one.
        }
        server.readPOSTparam(param, NAMELEN, value, VALUELEN); // This is where it goes wrong
        if(strcmp(param,"len") == 0) {
          rawLen = atoi(value); //value = 35
        }
      }
      Serial.println(rawCode); // outputs 35
      Serial.println(rawLen);  // outputs 35
      parseData(rawCode, rawLen);
    }
  }
}

exiva:
It just seems to hang the arduino.

strncpy(rawCode,"",15);

strncpy(rawCode,value,14);

rawCode is a pointer pointing to nothing until you change it. You can't copy to "nowhere" like that. Not surprised it hung. You need to read up on C strings and pointers. The suggested fix of changing it to a char array is one solution.

Yeah, sorry that was me loosing the plot for a moment. I realised a little while after posting that suggestion that it was daft :astonished: