• C


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"
Who is Participating?
Infinity08Connect With a Mentor Commented:
The sscanf function parses the given string (first argument) according to the given format string (second argument), and stores the extracted values at the given memory addresses (other arguments).
It returns the number of succesfully read tokens.

So, if it returns 1, it means that the first token (the %s token in this case) was read succesfully.
If it returns 0, it means that none of the tokens were read succesfully.

For more information on how sscanf works, I refer you to this reference page :

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.
evilrixSenior Software Engineer (Avast)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.