I'm trying to write function that will return me a value of variable passed by GET or POST but got problems declaring char variable.
Every variable identification will be two chars long ex. &ac=
Every variable value will be max ten chars long
I was debuging and debuging and as you can see below if I declare char like this:
char mth[] = "00";
Everything works fine and Serial output is like this:
&ac=ulogin&lg=qwe&ps=123
l0
lg
14
And it's OK output is just like it should be but if i declare char like this:
char mth[2]; //or
char mth[2] = {'\0', '\0'};//or
char mth[2] = {'a', 'a'} ;
Serial prints some odd characters and algorithm just don't work thanks to those strange extra characters:
&ac=ulogin&lg=qwe&ps=123
la
#h
lg
#h
0
My test code:
void setup() {
Serial.begin(9600);
char *st = "&ac=ulogin&lg=qwe&ps=123"; //some http var string for tests
findVarIndex(st, "lg"); //calling to search for &lg value
}
void loop() {
}
int findVarIndex(char *str, char *sfind) {
int fn = 0;
int in = 0;
char mth[] = "aa"; //<= core of the problem
Serial.println(str);
for(int i = 0; i < strlen(str); i++) {
if(str[i] == '&') {
for(int n = 0; n < strlen(sfind); n++) {
if(str[i+(n+1)] == sfind[n]) {
if(in < 2) {
mth[in] = str[i+(n+1)];
in++;
}
Serial.println(mth);
if(!strcmp(mth, sfind)) {
if(str[i+(n+1)+1] == '=') {
fn = (i+(n+1)+2);
break;
}
}
}
}
}
}
Serial.println(fn);
return fn;
}
I'm really thank full for any advice, I'm sure I just don't know something about char arrays in arduino.