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?

后端 未结 2 1535
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 16:50

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?

相关标签:
2条回答
  • 2021-01-26 17:02

    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.

    0 讨论(0)
  • 2021-01-26 17:04

    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.
    0 讨论(0)
提交回复
热议问题