Searching char array for string in C
Question
I have a string that looks like this:
"show net\r\r\nSSid=roving1\r\nChan=1\r\nAssoc=FAIL\r\nRate=12, 24Mb\r\nAuth=FAIL\r\nMode=NONE\r\nDHCP=FAIL\r\nBoot=0\r\nTime=OK\r\nLinks=0\r\n<4.00> "
This is data I receive from a peripheral device and I want to scan it for the string "Auth=FAIL". I'm querying it using the following command:
res = strstr(uart_rd, "Auth=FAIL");
however, it returns a value of 0. But if I simply issue the command:
res = strstr("nnAuth=FAILn", "Auth=FAIL");
it returns a value. What could be going wrong when it tries to search my char array uart_rd?
EDIT:
It looks like the issue was with a memset I was doing to clear the array before putting new data into it. I was filling it with 0s using the command:
memset(uart_rd,0,sizeof(uart_rd));
whereas I've now changed it to
memset(uart_rd,"",sizeof(uart_rd));
and it all seems to be working. Thanks for the help in diagnosing where my problem was though!
Solution
Well, this code
char uart_rd[] = "show net\r\r\nSSid=roving1\r\nChan=1\r\nAssoc=FAIL\r\nRate=12, 24Mb\r\nAuth=FAIL\r\nMode=NONE\r\nDHCP=FAIL\r\nBoot=0\r\nTime=OK\r\nLinks=0\r\n<4.00> ";
char *res;
res = strstr(uart_rd, "Auth=FAIL");
printf("%s", res);
gives this output:
Auth=FAIL
Mode=NONE
DHCP=FAIL
Boot=0
Time=OK
Links=0
<4.00>
So you better check your declarations of strings.