<div dir="ltr">On Tue, Oct 22, 2013 at 9:40 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve+comp.lang.python@pearwood.info" target="_blank">steve+comp.lang.python@pearwood.info</a>></span> wrote:<br><div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Tue, 22 Oct 2013 15:39:42 +0000, Grant Edwards wrote:<br>
<br>
>> No, I was thinking of an array. Arrays aren't automatically initialised<br>
>> in C.<br>
><br>
> If they are static or global, then _yes_they_are_. They are zeroed.<br>
<br>
</div>Not that I don't believe you, but do you have a reference for this?<br>
Because I keep finding references to uninitialised C arrays filled with<br>
garbage if you don't initialise them.<br>
<br>
Wait... hang on a second...<br>
<br>
/fires up the ol' trusty gcc<br>
<br>
<br>
[steve@ando c]$ cat array_init.c<br>
#include<stdio.h><br>
<br>
int main()<br>
{<br>
int i;<br>
int arr[10];<br>
for (i = 0; i < 10; i++) {<br>
printf("arr[%d] = %d\n", i, arr[i]);<br>
}<br>
printf("\n");<br>
return 0;<br>
}<br>
<br>
[steve@ando c]$ gcc array_init.c<br>
[steve@ando c]$ ./a.out<br>
arr[0] = -1082002360<br>
arr[1] = 134513317<br>
arr[2] = 2527220<br>
arr[3] = 2519564<br>
arr[4] = -1082002312<br>
arr[5] = 134513753<br>
arr[6] = 1294213<br>
arr[7] = -1082002164<br>
arr[8] = -1082002312<br>
arr[9] = 2527220 <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
What am I missing here?<br></blockquote><div><br></div><div>The array you made there is an auto variable (stack), not a static or a global. Try one of the following (neither has been tested):</div><div><br></div><div>Static:</div>
<div><br></div><div>int main()<br>{<br> int i;<br> static int arr[10];<br> for (i = 0; i < 10; i++) {<br> printf("arr[%d] = %d\n", i, arr[i]);<br> }<br> printf("\n");<br> return 0;<br>
}<br></div><div><br></div><div><br></div><div><br></div><div><div>Global:</div><div><br></div><div>int arr[10];<br></div><div>int main()<br>{<br> int i;<br> for (i = 0; i < 10; i++) {<br> printf("arr[%d] = %d\n", i, arr[i]);<br>
}<br> printf("\n");<br> return 0;<br>}</div></div><div><br></div><div>As for a reference: <a href="http://stackoverflow.com/questions/1831290/static-variable-initialization">http://stackoverflow.com/questions/1831290/static-variable-initialization</a> and <a href="http://stackoverflow.com/questions/3373108/why-are-static-variables-auto-initialized-to-zero">http://stackoverflow.com/questions/3373108/why-are-static-variables-auto-initialized-to-zero</a>, both of which then reference the C++ standard.</div>
</div><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><div class="h5"><br>
<br>
--<br>
Steven<br>
--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div></div>