Why passing references to sscanf doesn't work.

Discussion in 'C Programming' started by aj, Jun 25, 2007.

  1. aj

    aj Guest

    Can someone explain why this example works:

    bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
    int &opOct3, int &opOct4)
    {
    int b1, b2, b3, b4;
    unsigned char c;

    if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", &b1, &b2, &b3, &b4,
    &c) != 4)
    {
    return false;
    }

    if ((b1 | b2 | b3 | b4) > 255)
    {
    return false;
    }

    if (strspn(ipIpAddress, "0123456789.") < strlen(ipIpAddress))
    {
    return false;
    }

    opOct1 = b1;
    opOct2 = b2;
    opOct3 = b3;
    opOct4 = b4;

    return true;
    }







    but this one does not? (Segmentation Fault)

    bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
    int &opOct3, int &opOct4)
    {
    unsigned char c;

    if (sscanf(ipIpAddress, "%3i.%3i.%3i.%3i%c", opOct1, opOct2,
    opOct3, opOct4, &c) != 4)
    {
    return false;
    }

    if ((b1 | b2 | b3 | b4) > 255)
    {
    return false;
    }

    if (strspn(ipIpAddress, "0123456789.") < strlen(ipIpAddress))
    {
    return false;
    }

    return true;
    }



    In each case, I am passing references to int's to the sscanf
    function.

    Thanks,
    AJ
     
    aj, Jun 25, 2007
    #1
    1. Advertising

  2. aj

    Ben Pfaff Guest

    aj <> writes:

    > Can someone explain why this example works:
    >
    > bool SomeFunction(const char * ipIpAddress, int &opOct1, int &opOct2,
    > int &opOct3, int &opOct4)


    This is C++ code, so comp.lang.c++ would be a better place to ask
    about it.
    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
     
    Ben Pfaff, Jun 25, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ishekara
    Replies:
    2
    Views:
    504
    Eric Sosman
    Jun 23, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    910
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,999
    Smokey Grindel
    Dec 2, 2006
  4. aj
    Replies:
    4
    Views:
    326
    red floyd
    Jun 25, 2007
  5. Rob Campbell
    Replies:
    3
    Views:
    78
    Brian McCauley
    Jun 4, 2005
Loading...

Share This Page