![]() |
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 |
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 |
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 |
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 07:16 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.