I'm encountering rather strange behavior when preforming a sscanf. Currently working on a windows 7 machine in c.
I have the following:
if( sscanf( str, "%1[a-zA-Z]%31[a-zA-Z+.-]%n", &scheme[ 0 ], &scheme[ 1 ], &num_chars ) >= 1 )
{
return( num_chars );
}
The str variable is a large input string with potentially larger then 32 characters. The scheme variable is declared as an argument to the wrapping function call, it's a 32 character array.
I can easily do this with a couple of scanfs or two separate variables. I was just curious as to why this doesn't work as is.
Edit:
At the time I executed this and the error occurred str contained "tel-net" (was testing the '-') and it resulted in the scheme string having basically no usable characters.
Solution:
I figured out what the problem was, it was actually not a scanf issue at all.
This is how i declared the scheme variable:
IOP_uri_scheme_type * scheme_str;
IOP_uri_scheme_type was declared as follows:
typedef char IOP_uri_scheme_type[ IOP_URI_MAX_SCHEME_SZ ]; // Size = 32
The problem was the indexing, scheme[ 1 ] was actually jumping the entire block (all 32 bytes) rather then a character like i was expecting. So technically the scanf was written correctly to begin with (minus the %n thing).
One possible way i can solve this is by casting scheme as a (char *) first or directly manipulating the pointer value, de-referencing it, or just not using a pointer which i don't need anyways.
Thanks for everyone's help.