I found this unexpected behaviour when writing code for Arduino, and I was able to replicate the same thing in MSVC15:
struct IDCount
{
uint16_t count;
char ID[20];
};
void test06b()
{
IDCount item;
char str[] = "0411010103, 8";
char str1[20];
// writing to struct member char array
int res1 = sscanf(str, "%[^,], %d", item.ID, &item.count);
printf("[%d] id: [%s]\tcount: [%d]\n", res1, item.ID, &item.count);
// writing to a char array
int res2 = sscanf(str, "%[^,], %d", str1, &item.count);
printf("[%d] id: [%s]\tcount: [%d]\n", res2, str1, &item.count);
}
Results:
[2] id: [] count: [8]
[2] id: [0411010103] count: [8]
I spent quite a bit of time checking on the format specifier, before I narrowed down to the issue with the struct char array. Why isn't the char array in the struct not working? Any ideas?
Thanks in advance.
Update Changing from uint16_t to int works.