I was just wondering if you could clear something up for me.
Let's have some example code to explain my question:
#include <stdio.h>
int main(void)
{
char test[100];
printf("%s",test);
return 0;
}
If I am not totally mistaken, this should output randomly either some character that was at this memory address before I declared it or nothing if it was empty like in a virtual environment. So, this is my understanding. The memory held before I put something in is understood as a char and written to the terminal. For instance ascii 'a' = 97 = 01100001. That's why it outputs 'a'. Could have been anything else. Or nothing. And then it stops.
But if I put 'a' in the first position and then print it like this:
test[0] = 'a'
printf("%s",test);
It will output 'a' and additionally to that some character or nothing and then stop.
This is how I understand arrays: An array is a pointer to the first address and the brackets are a dereferences of the address after adding the number times sizeof(type) to it.
So, in that case, the random 01100001 (Ascii 'a') found in the memory in the first example should be indistinguishable for printf from the deliberately placed 01100001 (Ascii 'a') in the second example. Yet, when I run printf, I don't get 100 random outputs. I get one. And I don't assume random fields are in general set to '\0'.
Which means, my understanding must be wrong somewhere. Please help me understand where I make my mistake.