help with snippet from c-faq.com

Discussion in 'C Programming' started by Mark, Jan 13, 2011.

  1. Mark

    Mark Guest

    Hi

    take a look at http://c-faq.com/misc/hexio.html, specifically at:

    static char retbuf[33];
    char *p;
    p = &retbuf[sizeof(retbuf)-1];
    ....

    Why do we have to keep additional pointer 'p' to a 'retbuf'? If we fill in
    the 'retbuf' and then return it from the function ('return retbuf'), it will
    always point at the very first element of the buffer, and we can safely use
    from the caller.

    Am I wrong?

    --
    Mark
     
    Mark, Jan 13, 2011
    #1
    1. Advertising

  2. Mark

    Ben Pfaff Guest

    "Mark" <> writes:

    > take a look at http://c-faq.com/misc/hexio.html, specifically at:
    >
    > static char retbuf[33];
    > char *p;
    > p = &retbuf[sizeof(retbuf)-1];
    > ...
    >
    > Why do we have to keep additional pointer 'p' to a 'retbuf'? If we
    > fill in the 'retbuf' and then return it from the function ('return
    > retbuf'), it will always point at the very first element of the
    > buffer, and we can safely use from the caller.


    The function doesn't always fill in the whole buffer. Most of
    the time it only fills in the last few bytes. In such a case,
    returning retbuf would return garbage.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Jan 13, 2011
    #2
    1. Advertising

  3. Mark

    Ian Collins Guest

    On 01/13/11 01:01 PM, Mark wrote:
    > Hi
    >
    > take a look at http://c-faq.com/misc/hexio.html, specifically at:
    >
    > static char retbuf[33];
    > char *p;
    > p = &retbuf[sizeof(retbuf)-1];
    > ....
    >
    > Why do we have to keep additional pointer 'p' to a 'retbuf'? If we fill
    > in the 'retbuf' and then return it from the function ('return retbuf'),
    > it will always point at the very first element of the buffer, and we can
    > safely use from the caller.


    Read the reset of the function!

    The buffer is filled from the end.

    --
    Ian Collins
     
    Ian Collins, Jan 13, 2011
    #3
  4. Mark

    Mark Guest

    Ben Pfaff wrote:
    >> static char retbuf[33];
    >> char *p;
    >> p = &retbuf[sizeof(retbuf)-1];
    >> ...
    >>
    >> Why do we have to keep additional pointer 'p' to a 'retbuf'? If we
    >> fill in the 'retbuf' and then return it from the function ('return
    >> retbuf'), it will always point at the very first element of the
    >> buffer, and we can safely use from the caller.

    >
    > The function doesn't always fill in the whole buffer. Most of
    > the time it only fills in the last few bytes. In such a case,
    > returning retbuf would return garbage.


    I see now, function fills in the output buffer starting from its *end*.
    Thank you for clarifying this to me.

    --
    Mark
     
    Mark, Jan 13, 2011
    #4
    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. Kavitha Rao

    Help!-Errors while running this snippet code

    Kavitha Rao, Jul 2, 2003, in forum: C Programming
    Replies:
    2
    Views:
    757
    Kavitha Rao
    Jul 3, 2003
  2. Replies:
    13
    Views:
    577
    ajonesfl
    Jul 11, 2007
  3. Replies:
    19
    Views:
    566
    Alf P. Steinbach
    Jan 30, 2008
  4. JC

    Help with javascript snippet

    JC, Jan 25, 2007, in forum: Javascript
    Replies:
    1
    Views:
    105
    gg9h0st
    Jan 25, 2007
  5. Replies:
    5
    Views:
    105
    apatheticagnostic
    May 8, 2008
Loading...

Share This Page