I’ve tried everything Google-able, but can’t seem to wrap my head around this. I’m trying to return a char and pass to another function with no luck. Do I need to do some sort of memory location?
char hostname[] = "www.XXXXX.com";
uint8 ipaddr[] = {XXX,XXX,XXX,XXX};
char uri[] = "/api/Login/";
char key[] = API_KEY; //3490324032-asdfasd-234234
const int port = 80;
//function to combine uri and key
char combine(char key[], char uri[]) {
int i = 0;
int x = 0;
char long_s[sizeof(uri) + sizeof(key)];
while(uri[i]) {
long_s[i] = uri[i];
++i;
}
while(key[x]) {
long_s[i] = key[x];
++i;
++x;
}
long_s[i] = '\0';
//Serial.print(long_s);
return long_s; //pointer?
}
char lon = combine (key, uri);
char* newURI = &lon;
// A get request
GETrequest getSession(ipaddr, port, hostname, newURI);
Your function return value is
char– that means that it returns a singlechar(which can only represent one character). A string is represented by an array ofchars, which is usually referred to by a pointer (char *).So you need to return a
char *– but this leads to another problem. The pointer must be pointing at an array that outlives the function call. Your arraylong_swill be destroyed as soon as the function returns, so this is not suitable. The usual alternative is to allow the caller to supply a buffer of sufficient size (note also that your loops can be replaced with thestrcpy()andstrcat()functions):Then your caller must use: