You say the function is returning a char
, but yet you try to return a char*
(the reference to the array). So you either need to change the function to be a char*
, or you need to return an actual char
. Also, you return a reference to memory that is no longer valid, you need to either malloc on the heap, or pass in the pointer to be filled with the function call.
Edit: Further clarification
All of these are pointers to arrays of type uint8 or char.
char hostname[] = "www.XXXXX.com";
uint8 ipaddr[] = {XXX,XXX,XXX,XXX};
char uri[] = "/api/Login/";
char key[] = API_KEY; //3490324032-asdfasd-234234
This is regular data.
const int port = 80;
//function to combine uri and key
char combine(char key[], char uri[]) {
int i = 0;
int x = 0;
This next line declares a pointer to an array of chars that has 8 elements. This probably isn't what you want. The sizeof macro is evaluating to 4
because that is the size of a pointer (compile and run the program below). Also you need to change this to char * long_s = malloc(sizeof(uri) + sizeof(key));
Dont forget to free it later.
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);
This returns the pointer to the array that you just filled, not an actual char.
return long_s; //pointer?
}
These next lines should just be char * newURI = combine(key, uri);
char lon = combine (key, uri);
char* newURI = &lon;
// A get request
GETrequest getSession(ipaddr, port, hostname, newURI);
Edit: Try this out:
#include <stdio.h>
void test(char k[])
{
printf("%d\n", sizeof(k));
}
int main ()
{
char* key[100];
test(key);
printf("%d\n", sizeof(key));
return 0;
}