Link to home
Start Free TrialLog in
Avatar of dpalyca755
dpalyca755

asked on

SSCANF

What does this piece of code do?
It is a condition in a For Loop.
What does ==1 mean and inversely what would a 0 mean?

"sscanf(route_parameter+net_offset, "%s %n", &wpt_name[x][0], &offset) == 1"
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It has a string route_parameter that contains a series of "string number" combinations.

net_offset is obviously a numeric value that gets updated within the loop to point to the next combination.

After each successful loop (ie. the result of sscanf == 1) the wpt_name array entry at position x in the array contains the string value.
The offset variable will contain the number value from the combination.  What it is will be relative to the application.

> sscanf(route_parameter+net_offset, "%s %n", &wpt_name[x][0], &offset) == 1
%s will read the string (that is stored at route_parameter+net_offset) upto first space.
And this will be stored in wpt_name[x][0].
%n will keep the count of number of characters eaten by %s and that will get stored in offset.

So, if the string stroed in route_parameter is "string 1234",
%s will read 's', 't', 'r', 'i', 'n', 'g' and after this there is a space. So, including space, the number of characters read is 7. So, the offset will get the value 7.

> What does ==1 mean and inversely what would a 0 mean?
And sscanf() returns the number of input items successfully matched.
Now, in the sscanf() that you have, there is only one %s and that is the only one to be matched,
If the string at route_parameter+net_offset is a null string, then %s will not read anything and sscanf will return 0. If there is a string, then it will return 1.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.