简体   繁体   English

使用atoi将char字符串化为int

[英]Tokenized string of char to ints using atoi

I am trying to take user input: (1 345 44 23) and make it into a tokenized char string then into ints. 我正在尝试接受用户输入:(1 345 44 23)并使其成为标记化的char字符串,然后转换为ints。 Surprisingly I could not find much help for what I would think would be a common task. 令人惊讶的是,对于我认为这是常见任务的工作,我找不到太多帮助。

Any ideas how to convert the char string into an in string using tokens? 任何想法如何使用令牌将char字符串转换为in字符串?

My program crashes when it gets to the conversion (after the tokenization [I realize this is not a word]). 我的程序在进行转换时崩溃(在标记化之后[我意识到这不是一个字])。

Thanks! 谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81


void strInput (char str[], int maxChars);
void custatoi(char * tokenArray[], int * data, int numOfTok);


int main(int argc, char *argv[])
{
    char str[StrSZE];
    char* tokenArray;
    int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
    int* data;


    strInput(str, maxChars);


    tokenArray = strtok(str, " \t");
    while (tokenArray)
    {
        printf("token: %s\n", tokenArray);
        tokenArray = strtok(NULL, " \t");
        numOfToken++;
    }


    data = (int *) malloc(numOfToken * sizeof(int));

    custatoi(tokenArray, data, numOfToken);

    system("PAUSE");
    return 0;
}



void strInput (char str[], int maxChars)
{
    char garbage;
    int k=0;

    str[0]='\0';

    printf("Please type a string of whole numbers (intigers).\n\n");

    while ((k<80) && ((str[k] = getchar()) != '\n'))
        k++;

    /* Clears the keyboard buffer.  */
    if (k==80)
        while((garbage = getchar()) != '\n')
            ;

    /* Place null at the end of the line read in from user */
    str[k]='\0';

    printf("str after input is: %s\n\n", str);
}


void custatoi(char * tokenArray[], int * data, int numOfTok)
{
    int i;

    for (i=0; i < numOfTok; i++)
        data[i] = atoi(tokenArray[i]);
}

At the end of the strtok loop, tokenArray will be set to NULL . strtok循环结束时, tokenArray将设置为NULL You then pass it to custatoi , which presumably crashes when it tries to dereference it. 然后,将其传递给custatoi ,它可能会在尝试取消引用时崩溃。

Note that tokenArray is not an array of strings; 注意tokenArray不是字符串数组; it's just a single string pointer (or a pointer to an array of characters). 它只是单个字符串指针(或指向字符数组的指针)。 If you want to accumulate the tokens into an array, you'll have to create a separate array for that purpose. 如果要将令牌累积到数组中,则必须为此创建一个单独的数组。

I corrected the errors in yours code: There was some mistakes in main(), tokenArray data type was not correct. 我更正了您代码中的错误: mistakes in main(), tokenArray data type was not correct.存在一些mistakes in main(), tokenArray data type was not correct.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81


void strInput (char str[], int maxChars);
void custatoi(char*  tokenArray[], int * data, int numOfTok);


int main(int argc, char *argv[])
{
    char str[StrSZE];


    int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
    int* data;
    char* tokenArray[50];    // Declared correctly 

    strInput(str, maxChars);


    tokenArray[i] = strtok(str, " \t");   // Also made a change here!
    while (tokenArray[i])
    {
        printf("token: %s\n", tokenArray[i]);
        i++;
        tokenArray[i] = strtok(NULL, " \t");
        numOfToken++;
    }


    data = (int *) malloc(numOfToken * sizeof(int));

    custatoi(tokenArray, data, numOfToken);

    printf("data\n");
    for(i=0;i<numOfToken;i++){
        printf(" %d\n",data[i]);

    }

    system("PAUSE");
    return 0;
}



void strInput (char str[], int maxChars)
{
    char garbage;
    int k=0;

    str[0]='\0';

    printf("Please type a string of whole numbers (intigers).\n\n");

    while ((k<80) && ((str[k] = getchar()) != '\n'))
        k++;

    /* Clears the keyboard buffer.  */
    if (k==80)
        while((garbage = getchar()) != '\n')
            ;

    /* Place null at the end of the line read in from user */
    str[k]='\0';

    printf("str after input is: %s\n\n", str);
}


void custatoi(char*  tokenArray[], int * data, int numOfTok)
{
    int i;

    for (i=0; i < numOfTok; i++)
        data[i] = atoi(tokenArray[i]);
}

The main problem is that custatoi() expects to work with an array of pointers to char , while tokenArray in main() is a mere pointer to char . 主要问题是custatoi()希望使用指向char的指针数组,而main()中的tokenArray仅仅是指向char指针。 The original code never collects all pointers to tokens in the input string into an array that custatoi() expects, there isn't such an array in the original code. 原始代码永远不会将指向输入字符串中标记的所有指针收集到custatoi()期望的数组中,原始代码中没有这样的数组。

Please study the fixed code: 请研究固定代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define StrSZE 81

void custatoi(char* tokenArray[], int* data, int numOfTok);

int main(void)
{
    char str[StrSZE];
    char** tokenArray;
    int numOfToken = 0, i;
    int* data;

    //strInput(str, maxChars);
    strcpy(str, "1 345 44 23");

    tokenArray = malloc(sizeof(char*));

    tokenArray[numOfToken] = strtok(str, " \t");
    while (tokenArray[numOfToken] != NULL)
    {
        printf("token: %s\n", tokenArray[numOfToken]);
        numOfToken++;
        tokenArray = realloc(tokenArray, sizeof(char*) * (numOfToken + 1));
        tokenArray[numOfToken] = strtok(NULL, " \t");
    }

    data = malloc(numOfToken * sizeof(int));

    custatoi(tokenArray, data, numOfToken);

    for (i = 0; i < numOfToken; i++)
      printf("data[%d]=%d\n", i, data[i]);

    return 0;
}

void custatoi(char* tokenArray[], int* data, int numOfTok)
{
    int i;

    for (i=0; i < numOfTok; i++)
        data[i] = atoi(tokenArray[i]);
}

Output ( idone ): 输出( idone ):

token: 1
token: 345
token: 44
token: 23
data[0]=1
data[1]=345
data[2]=44
data[3]=23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM