jeudi 9 mars 2017

For random generating string how to fix "for loop initial declaration used outside c99 mode"?

Random string generator using C programming. How to fix "for loop initial declaration used outside c99 mode"?

void generate_string(char **string){

    srand(time(NULL));
    int numOfChars=rand()%9+1;
    int i=0;
    char temp[numOfChars];
     for(i=0;i<numOfChars;i++){
        char tmp ='A'+rand()%26;
        temp[i]=tmp;

     }
     temp[i+1]='\0';
    strcpy(*string,temp);
}


    int main()
    {
    char *str;
    str= (char*)malloc(11 * sizeof(char));
    generate_string(&str);
    int len=(int)strlen(str);
        printf("Random String:");
    for(int i=0;i<len;i++) { 
        printf("%c",*str);
        str++;
       } 
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire