help with snippet from c-faq.com

M

Mark

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?
 
B

Ben Pfaff

Mark said:
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.
 
I

Ian Collins

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.
 
M

Mark

Ben said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top