質問

I am trying to take user input: (1 345 44 23) and make it into a tokenized char string then into 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?

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]);
}
役に立ちましたか?

解決

I corrected the errors in yours code: There was some 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]);
}

他のヒント

At the end of the strtok loop, tokenArray will be set to NULL. You then pass it to custatoi, which presumably crashes when it tries to dereference it.

Note that tokenArray is not an array of strings; 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.

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. 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.

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):

token: 1
token: 345
token: 44
token: 23
data[0]=1
data[1]=345
data[2]=44
data[3]=23
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top