Hello all,
I have a doubt. It's about Jesus Alvarez's my_strncpy function.
Is it correct not to close the array? Though I see, it is initialized by '\0 '.
Is this the normal behavior on declaring a character array?
Greetings,
Jose G. López
- Hi Jose,
- Welcome to the wiki!
- As you can probably tell, this wiki is very inactive, so you might not get many responses to your questions. I haven't coded C in quite a while, so I'm not confident in my reply, but I'll give it a go anyway, given that other responses might not be so forthcoming.
- Firstly, the failure to terminate the destination string in Jesus Alvarez's my_strncpy function appears to me to be a bug.
- Secondly, I once wrote this note to myself about variable initialisation, although I can't recall my sources, so you might want to research to confirm that it's correct: "Static and global variables are set to their representation of zero or NULL; automatic variables (i.e. as part of a non-file-scope block, such as within a function) are not initialised." In other words, if my note is correct, then the buf_1 array is in fact not initialised at all, and any seeming initialisation to '\0' is a happy accident. You might want to post to comp.lang.c for more expert opinions though: as I mentioned, I'm not really up on the finer points of C at the moment.
- --Laird (Netocrat) 06:52, 16 April 2010 (UTC)
- Thank you very much Laird,
- You were very helpful. I found this quote in K&R2, page 39:
- External and static variables are initialized to zero by default. Automatic variables
- for which is no explicit initializer have undefined (i.e.,garbage) values.
- --Jose G. López (Josgalo)