PLEASE HELP - Odd problem passing function arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpptutor2000@yahoo.com

    #1

    PLEASE HELP - Odd problem passing function arguments

    Could some C guru please help me?
    I have a function that takes as an argument a pointer to an array of
    unsigned chars (basically a hex representation of a dotted decimal IP
    address). When I print out the received values in the receiving
    function, I get something completely different from what I passed in.

    The following are the relevant code snippets:
    In the calling function:

    unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
    ......
    ......
    returnStatus = DoSomething(Tem pAddrs);

    In the called function I have, right at the start:

    printf("Receive d : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
    addr[3]);

    I am sure there is a problem, but I am not sure what it is - thanks in
    advance for your help.

  • jacob navia

    #2
    Re: PLEASE HELP - Odd problem passing function arguments

    cpptutor2000@ya hoo.com wrote:
    Could some C guru please help me?
    I have a function that takes as an argument a pointer to an array of
    unsigned chars (basically a hex representation of a dotted decimal IP
    address). When I print out the received values in the receiving
    function, I get something completely different from what I passed in.
    >
    The following are the relevant code snippets:
    In the calling function:
    >
    unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
    .....
    .....
    returnStatus = DoSomething(Tem pAddrs);
    >
    In the called function I have, right at the start:
    >
    printf("Receive d : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
    addr[3]);
    >
    I am sure there is a problem, but I am not sure what it is - thanks in
    advance for your help.
    >
    You are passing character strings and printing unsigned integers.

    Either you change the printfs to %s or pass the numbers, and not
    character strings!


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32

    Comment

    • Eric Sosman

      #3
      Re: PLEASE HELP - Odd problem passing function arguments

      cpptutor2000@ya hoo.com wrote On 10/05/07 17:26,:
      Could some C guru please help me?
      I have a function that takes as an argument a pointer to an array of
      unsigned chars (basically a hex representation of a dotted decimal IP
      address). When I print out the received values in the receiving
      function, I get something completely different from what I passed in.
      >
      The following are the relevant code snippets:
      In the calling function:
      >
      unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
      .....
      .....
      returnStatus = DoSomething(Tem pAddrs);
      >
      In the called function I have, right at the start:
      >
      printf("Receive d : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
      addr[3]);
      >
      I am sure there is a problem, but I am not sure what it is - thanks in
      advance for your help.
      The first line requires a diagnostic from the compiler.

      So does the second.

      And the third.

      There's not enough information given to tell whether
      there's anything wrong with the fourth line. It might
      be a hopeless botch or it might be just fine; I can't
      say.

      The same holds for the final line: You've snipped
      away so much that it's not possible to tell whether the
      line is right or wrong.

      Please post real code for a short, complete, compilable
      program that demonstrates your problem. As things stand,
      all I can say is that your difficulty is ..... or something
      along those general lines.

      --
      Eric.Sosman@sun .com

      Comment

      • Ben Pfaff

        #4
        Re: PLEASE HELP - Odd problem passing function arguments

        "cpptutor2000@y ahoo.com" <cpptutor2000@y ahoo.comwrites:
        In the calling function:
        >
        unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
        .....
        .....
        returnStatus = DoSomething(Tem pAddrs);
        >
        In the called function I have, right at the start:
        >
        printf("Receive d : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
        addr[3]);
        The elements of TempAddrs are strings, or, more precisely,
        pointers to the first element of an array of char.

        %u expects a unsigned int argument.

        You can't mix those types like that.
        --
        Here's a tip: null pointers don't have to be *dull* pointers!

        Comment

        • Keith Thompson

          #5
          Re: PLEASE HELP - Odd problem passing function arguments

          "cpptutor2000@y ahoo.com" <cpptutor2000@y ahoo.comwrites:
          Could some C guru please help me?
          I have a function that takes as an argument a pointer to an array of
          unsigned chars (basically a hex representation of a dotted decimal IP
          address). When I print out the received values in the receiving
          function, I get something completely different from what I passed in.
          >
          The following are the relevant code snippets:
          In the calling function:
          >
          unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
          .....
          .....
          returnStatus = DoSomething(Tem pAddrs);
          >
          In the called function I have, right at the start:
          >
          printf("Receive d : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
          addr[3]);
          >
          I am sure there is a problem, but I am not sure what it is - thanks in
          advance for your help.
          You haven't shown us the declaration of DoSomething.

          TempAddrs is an array of pointers to unsigned char. Evaluating the
          name ``TempAddrs'' as a function argument gives you a pointer to
          pointer to unsigned char. Is that what DoSomething expects to
          receive? If not, turn up your compiler's warning level until it
          complains.

          You're representing each byte of the IP address as a string; for
          example, you're representing the decimal value 192 as the character
          sequence { '0', 'x', 'C', '0', '\0' }. You probably just want to
          represent it as an unsigned char with the value 0xC0 (which you can
          also write as 192); then a full IP address can be represented as an
          array of four unsigned chars.

          See also section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Richard

            #6
            Re: PLEASE HELP - Odd problem passing function arguments

            Eric Sosman <Eric.Sosman@su n.comwrites:
            cpptutor2000@ya hoo.com wrote On 10/05/07 17:26,:
            >Could some C guru please help me?
            >I have a function that takes as an argument a pointer to an array of
            >unsigned chars (basically a hex representation of a dotted decimal IP
            >address). When I print out the received values in the receiving
            >function, I get something completely different from what I passed in.
            >>
            >The following are the relevant code snippets:
            >In the calling function:
            >>
            >unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
            >.....
            >.....
            >returnStatus = DoSomething(Tem pAddrs);
            >>
            >In the called function I have, right at the start:
            >>
            >printf("Receiv ed : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
            >addr[3]);
            >>
            >I am sure there is a problem, but I am not sure what it is - thanks in
            >advance for your help.
            >
            The first line requires a diagnostic from the compiler.
            >
            So does the second.
            >
            And the third.
            >
            There's not enough information given to tell whether
            there's anything wrong with the fourth line. It might
            be a hopeless botch or it might be just fine; I can't
            say.
            >
            The same holds for the final line: You've snipped
            away so much that it's not possible to tell whether the
            line is right or wrong.
            >
            Please post real code for a short, complete, compilable
            program that demonstrates your problem. As things stand,
            all I can say is that your difficulty is ..... or something
            along those general lines.
            What does %u mean in a printf? It seems clear what the problem is
            without a compilable sample - but maybe I am missing something?

            He needs to use %s.

            Comment

            • jacob navia

              #7
              Re: PLEASE HELP - Odd problem passing function arguments

              Richard wrote:
              >
              What does %u mean in a printf? It seems clear what the problem is
              without a compilable sample - but maybe I am missing something?
              >
              He needs to use %s.
              That's what I told him. Eric's answer is not really helpful.

              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatique
              http://www.cs.virginia.edu/~lcc-win32

              Comment

              • Eric Sosman

                #8
                Re: PLEASE HELP - Odd problem passing function arguments

                Richard wrote On 10/05/07 18:08,:
                >
                >>cpptutor2000@ yahoo.com wrote On 10/05/07 17:26,:
                >>>
                >>>In the called function I have, right at the start:
                >>>
                >>>printf("Rece ived : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
                >>>addr[3]);
                >
                [...]
                >
                What does %u mean in a printf?
                "Print an unsigned int."
                It seems clear what the problem is
                without a compilable sample - but maybe I am missing something?
                Perhaps you're missing the declaration of `addr'?
                So are all the rest of us ...
                He needs to use %s.
                Maybe. I'm not sure.

                --
                Eric.Sosman@sun .com

                Comment

                • Richard

                  #9
                  Re: PLEASE HELP - Odd problem passing function arguments

                  Eric Sosman <Eric.Sosman@su n.comwrites:
                  Richard wrote On 10/05/07 18:08,:
                  >>
                  >>>cpptutor2000 @yahoo.com wrote On 10/05/07 17:26,:
                  >>>>
                  >>>>In the called function I have, right at the start:
                  >>>>
                  >>>>printf("Rec eived : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
                  >>>>addr[3]);
                  >>
                  >[...]
                  >>
                  >What does %u mean in a printf?
                  >
                  "Print an unsigned int."
                  It was a rhetorical question :-)

                  Comment

                  Working...