I have a simple error that I know lies underneath my C code's memory usage, because the iterator in a simple for loop changes drastically after receiving user input on the command line:
int i = 1;
char input[] = "";
for (i = 1; i <= 5; i++) {
printf("i %d\n> ", i);
scanf("%s", input);
printf("input %s\ni %d\n", input, i);
}
The output should be simple enough:
i 1
> <receive input>
input <input>
i 1
to be repeated 5 times.
However, the iterator 'i' changes to anything but what is expected when any input is received.
An example output:
i 1
> 45
input 45
i 53
I have a hunch that this comes from memory access in the compiler. Anything helps!
'\0'(ASCII0) That requires 1 additional character at the end of your string in addition to each of the characters you intend to store. (that's how functions know where the end of your string is...)char input[] = "";declares and array of 1-char initialized to the empty-string (just the'\0'character). Once declared the array size is fixed, so it can never store a string (other than empty-string) and at most can store 1 character (byte).