PLEASE HELP - Odd problem passing function arguments

Discussion in 'C Programming' started by cpptutor2000@yahoo.com, Oct 5, 2007.

  1. Guest

    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(TempAddrs);

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

    printf("Received : %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.
     
    , Oct 5, 2007
    #1
    1. Advertising

  2. jacob navia Guest

    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(TempAddrs);
    >
    > In the called function I have, right at the start:
    >
    > printf("Received : %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
     
    jacob navia, Oct 5, 2007
    #2
    1. Advertising

  3. Eric Sosman Guest

    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(TempAddrs);
    >
    > In the called function I have, right at the start:
    >
    > printf("Received : %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, Oct 5, 2007
    #3
  4. Ben Pfaff Guest

    "" <> writes:

    > In the calling function:
    >
    > unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
    > .....
    > .....
    > returnStatus = DoSomething(TempAddrs);
    >
    > In the called function I have, right at the start:
    >
    > printf("Received : %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!
     
    Ben Pfaff, Oct 5, 2007
    #4
  5. "" <> writes:
    > 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(TempAddrs);
    >
    > In the called function I have, right at the start:
    >
    > printf("Received : %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_Keith) <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"
     
    Keith Thompson, Oct 5, 2007
    #5
  6. Richard Guest

    Eric Sosman <> writes:

    > 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(TempAddrs);
    >>
    >> In the called function I have, right at the start:
    >>
    >> printf("Received : %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.
     
    Richard, Oct 5, 2007
    #6
  7. jacob navia Guest

    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
     
    jacob navia, Oct 5, 2007
    #7
  8. Eric Sosman Guest

    Richard wrote On 10/05/07 18:08,:
    >
    >> wrote On 10/05/07 17:26,:
    >>>
    >>>In the called function I have, right at the start:
    >>>
    >>>printf("Received : %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, Oct 5, 2007
    #8
  9. Richard Guest

    Eric Sosman <> writes:

    > Richard wrote On 10/05/07 18:08,:
    >>
    >>> wrote On 10/05/07 17:26,:
    >>>>
    >>>>In the called function I have, right at the start:
    >>>>
    >>>>printf("Received : %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 :)
     
    Richard, Oct 5, 2007
    #9
    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. Gandu
    Replies:
    1
    Views:
    379
    Jose Rubio
    Dec 18, 2003
  2. KK
    Replies:
    2
    Views:
    711
    Big Brian
    Oct 14, 2003
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,157
    Richard Heathfield
    Feb 18, 2007
  4. jmborr
    Replies:
    1
    Views:
    455
    Stargaming
    Nov 3, 2007
  5. oldyork90
    Replies:
    10
    Views:
    369
    Jorge
    Sep 27, 2008
Loading...

Share This Page