Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   help with snippet from c-faq.com (http://www.velocityreviews.com/forums/t741928-help-with-snippet-from-c-faq-com.html)

Mark 01-13-2011 12:01 AM

help with snippet from c-faq.com
 
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


Ben Pfaff 01-13-2011 12:07 AM

Re: help with snippet from c-faq.com
 
"Mark" <mark_cruzNOTFORSPAM@hotmail.com> 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

Ian Collins 01-13-2011 12:09 AM

Re: help with snippet from c-faq.com
 
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

Mark 01-13-2011 12:14 AM

Re: help with snippet from c-faq.com
 
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



All times are GMT. The time now is 08:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.