Posted on 2011-04-26
Last Modified: 2012-06-21
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"
Question by:dpalyca755
    LVL 53

    Accepted Solution

    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 :

    LVL 17

    Expert Comment

    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.

    LVL 8

    Expert Comment

    > 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.
    LVL 39

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now