??? 10/04/11 16:18 Read: times |
#184023 - Another static question... |
I've been meanng to ask this for a while now.
Page 113 of K&R second edition gives the following example code. /* month_name: return name of n-th month */ char *month_name(int n) { static char *name[] = { "Illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return (n < 1 || n > 12) ? name[0] : name[n]; } What I don't understand is why char *name is declared static. I (think) I understand the static keyword. It restricts scope to the current procedure, and it provides permanent storage within a function. Neither appears to be a requirement in this example, or am I missing something? |
Topic | Author | Date |
Another static question... | 01/01/70 00:00 | |
WIthout Static In This Case | 01/01/70 00:00 | |
Thank you | 01/01/70 00:00 | |
It is worse than that | 01/01/70 00:00 | |
could add 'const' | 01/01/70 00:00 | |
That's the key! | 01/01/70 00:00 | |
Thanks Neil | 01/01/70 00:00 | |
True - but not quite the point here | 01/01/70 00:00 | |
isn't it explained in the book itself? | 01/01/70 00:00 | |
Yes, it is | 01/01/70 00:00 | |
Yes, but... | 01/01/70 00:00 | |
If the pupil does not understand... | 01/01/70 00:00 | |
In this instance... | 01/01/70 00:00 | |
That is why the call it learning. | 01/01/70 00:00 | |
isn't this just another example of .... | 01/01/70 00:00 | |
Information-hiding | 01/01/70 00:00 | |
not arguing that part | 01/01/70 00:00 | |
Not just protection. Also better overview (and reuse) | 01/01/70 00:00 |