buil a function with a part of a program

Hi,

I have a piece of code that I want to convert into a function

void getConfig() {
  File myFile = FileSystem.open("/mnt/sda1/config.json", FILE_READ);
  int jlen = myFile.size();
  int i = 0;
  char json[400];
  if (myFile) {
    while (myFile.available()) {
      char c = myFile.read();
      json[i] = c;
      i++;
    }
  }

  StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);
  if (!root.success()) {
    Serial.println("Parsing fail");
    return;
  }

  JsonObject& url = root["url"];
  const char *_host = url["host"];
  const char *_apiVersion = url["apiVersion"];
  const char *_resources = url["resources"];

  JsonObject& destinations = root["destinations"];
  const char *_stationFrom = destinations["from"];
  const char *_stationTo = destinations["to"];

  JsonObject& configs = root["configs"];
  const char *_limit = configs["limit"];
  const char *_fields = configs["fields"];

  const char *cca = _host;
  char *var = "?from=";


  int ccalen = strlen(cca);
  int varlen = strlen(var);
  int totallen = ccalen + varlen;
  char newText[totallen];
  strncpy(newText, _host, totallen);
  strcat(newText, var);


  Serial.println(newText);
}

output from newText is:

http://transport.opendata.ch?from=

thats ok..

and now with function:

char * catcat(const char *cca, char *var) {
  int ccalen = strlen(cca);
  int varlen = strlen(var);
  int totallen = ccalen + varlen;
  char newText[totallen];
  strncpy(newText, cca, totallen);
  strcat(newText, var);
  return newText;
}

void getConfig() {
  File myFile = FileSystem.open("/mnt/sda1/config.json", FILE_READ);
  int jlen = myFile.size();
  int i = 0;
  char json[400];
  if (myFile) {
    while (myFile.available()) {
      char c = myFile.read();
      json[i] = c;
      i++;
    }
  }

  StaticJsonBuffer<300> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);
  if (!root.success()) {
    Serial.println("Parsing fail");
    return;
  }

  JsonObject& url = root["url"];
  const char *_host = url["host"];
  const char *_apiVersion = url["apiVersion"];
  const char *_resources = url["resources"];

  JsonObject& destinations = root["destinations"];
  const char *_stationFrom = destinations["from"];
  const char *_stationTo = destinations["to"];

  JsonObject& configs = root["configs"];
  const char *_limit = configs["limit"];
  const char *_fields = configs["fields"];


  char *newUrl;
  newUrl = catcat(_host, "?from=");
  Serial.println(newUrl);



  ...

}

then the output is:

http://tra��a��Da�	�,a�a�9

I think I did something wrong on the return part.

some one has a idea pls.

THX

I think I did something wrong on the return part.

You returned a pointer to an array that has gone out of scope.
Do not pass GO, do not collect £200?

Maybe all that string copying and concatenation isn't necessary.

  char newText[totallen];
  strncpy(newText, cca, totallen);
  strcat(newText, var);
  return newText;

newText is local to the function. Returning an array is not possible. Returning a pointer to local storage is possible, but useless as the pointed to memory can be reused immediately.

Allocate an array large enough to hold both strings. Pass that array to the function to populate.

PaulS:
Allocate an array large enough to hold both strings. Pass that array to the function to populate.

ahh OK, got it.