When C says start up values of global variables are zero, does it mean also struct members? And what is the initial value of a pointer?
When C says start-up values of global[/static] variables are zero, does it mean also struct members? And what is the initial value of a [开发者_运维百科global/static] pointer? NULL?
Yes, this is specified by C99 6.7.8p10:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
Yes, all static variables, of whatever type, will be set to zero. That includes pointers - a NULL pointer is a pointer that is set to zero.
精彩评论