开发者

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?

开发者 https://www.devze.com 2023-01-30 17:32 出处:网络
When C says start-up values of global[/static] variables are zero, does it mean also structmembers? And what is the initial value of a [开发者_运维百科global/static] pointer? NULL?Yes, this is specifi

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消