0

Before I use ctime_r,I defined char* t instead of char* buf[32] by mistake.I thought char* t is point to NULL,and there will be a error after using ctime_r,like stycpy(t,"hello");.But I was surprised to find that char* t is not point to NULL as usual.Why?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Does this answer your question? [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – Elijan9 Jun 15 '20 at 08:41

1 Answers1

1

I thought char* t is point to NULL,

No, that's not true. There is no universal rule for initialization, it depends on the storage duration of the object.

In your case, most likely, the variable is a local scope one and has automatic storage duration, hence, unless initialized explicitly, the initial content is indeterminate.

Quoting C11, chapter 6.7.9/P10

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Oh,I asked a silly question A variable defined within the function is automatic,I knew that a long time ago.Maybe I use 'strcpy' recently,the char* t is often point to 'NULL' which misled and confused me .Thank you for your professional answer to a stupid question!Sorry to trouble you! – jone sterling Jun 15 '20 at 08:54