I am re-studying C right now and I have always been told that in C we have to define which maximum size would have an array so it can have enough space reserved in the memory. I mean this:
#include <stdlib.h> //Para función malloc
int main() {
int *enteros = malloc( sizeof(int) * 10 ); //An array of 10 integers
int i;
for(i = 0; i < 100; i++) {
enteros[i] = i*10;
printf( "-> enteros[%d] = %d\n", i, i*10 );
}
}
However, the following code works well (here I am not telling the max size, I just allocate enough memory for the pointer to int), and I just can't find an answer to what it's happening here.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *enteros = malloc( sizeof(int) );
int i;
for(i = 0; i < 2000; i++) {
*(enteros++) = i*10;
printf( "-> enteros[%d] = %d\n", i, i*10 );
enteros++;
}
}
It seems that I just can manage dynamic arrays perfectly well, without having to reallocate memory for the array and being forced to introduce a starting size, however I feel that I'm missing something.