Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help with snippet from c-faq.com

Reply
Thread Tools

help with snippet from c-faq.com

 
 
Mark
Guest
Posts: n/a
 
      01-13-2011
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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-13-2011
"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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-13-2011
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
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      01-13-2011
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help - can anyone tell me how to get this code snippet to work lawrencef@hlac.com.au Javascript 5 05-08-2008 06:28 PM
Memory Leaks - Can you help me find them in ths snippet nmehring@gmail.com C++ 19 01-30-2008 10:38 AM
Help with code snippet cougre.j@gmail.com C++ 13 07-11-2007 01:29 AM
Help with javascript snippet JC Javascript 1 01-25-2007 07:06 AM
Help!-Errors while running this snippet code Kavitha Rao C Programming 2 07-03-2003 09:15 AM



Advertisments