Convert char array to a int number in C

前端 未结 5 1083
梦毁少年i
梦毁少年i 2020-11-30 01:32

I want to convert a char array[] like:

char myarray[4] = {\'-\',\'1\',\'2\',\'3\'}; //where the - means it is negative

So it should be the

5条回答
  •  囚心锁ツ
    2020-11-30 01:59

    It's not what the question asks but I used @Rich Drummond 's answer for a char array read in from stdin which is null terminated.

    char *buff;
    size_t buff_size = 100;
    int choice;
    do{
        buff = (char *)malloc(buff_size *sizeof(char));
        getline(&buff, &buff_size, stdin);
        choice = atoi(buff);
        free(buff);
                        
    }while((choice<1)&&(choice>9));
    

提交回复
热议问题