The Code is supposed to count the amount of times a substring accours in a longer string and detect the location of the substring in the longer string. My Code seems to work but I am wondering why it worked even without allocating memory to index. Intuitvely i thought that because its not clear how many elements index has i would have to allocate memory to it. I appreciate your answers.
#include <stdio.h>
#include <string.h>
int *count_substr2(char *str, char *substr){
    int i;
    int counter= 0;
    int *index;
    for(i=0; i< (strlen(str)-1); i++){
        if(str[i]== substr[0] && str[i+1]==substr[1]){
            index[counter]=i;
            counter++;
        }
    }
    return index;
}
int main () {
    
    char *string= "abqervbqyyxöiböxxivxybaböxxrnvrixmxcxpäxyeyoymäab";
    char *substr= "ab";
    int* y =count_substr2(string, substr);
    printf("%d     %d      %d    ", y[0], y[1], y[2]);
    return 0;  
}
 
     
    