compiling with gcc C99
I am trying to compare 2 string using string compare. However, I seem to be getting a stack dump on the strcmp line.
**attribute will contain these, so I am looking for frametype.
[name] [time] [type] [time] [name] [callref] [type] [string] [name] [port] [type] [int16] [name] [frametype] [type] [int16]
Is this correct way to compare.
Many thanks for any suggestions,
void g_start_element(void *data, const char *element, const char **attribute) { for(i = 0; attribute[i]; i++) { /* Only interested in the frametype */ if(strcmp(attribute[i], 'frametype') == 0) { /* do some work here */ } } }
The context of this code is expat parsing – see this post. The attributes array is alternating name and value, with a single 0 to terminate.
Unless you’re looking for any attribute whose name or value is equal to your test string (which would be somewhat unusual), then your code should increment i by 2 rather than 1 – so that it steps over both a name and a value.
You should be comparing either attribute[i] to match a name, or or attribute[i+1] to match a value.
Don’t assume that the attributes will be in any particular order. Currently you’re only looking at attribute1, which is the value of the first attribute returned. If there are more than one attribute, they can be returned in any order.