Considering the example at http://c-faq.com/misc/hexio.html , what is the reason to have an additional pointer to a 'static' character buffer? Why can't we get away with retbuf
?
Without the static
keyword, the buffer would be allocated on the stack -- and deallocated by the time the function returns to the caller.
Using static
ensures the buffer is valid after the function returns.
You need a pointer so you can store a changing address. If you just had retbuf
, you would have to design the function to use a changing index variable. Eg:
int ind = sizeof(retbuf)-1;
retbuf[ind] = '\0';
etc.
Note that arrays are not pointers. An array is a fixed-size region of memory. A pointer is an address.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.