3

I want to write a programm that converts a string with numbers ("1 2 3") into an integer array. But strtok() doesn't return a value. Why not? My console output is just empty.

Edit: I get no error message.

This is my code:

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

int* string_to_array(char * string, int * myArray);

int main(void) {
    int* myArray = 0;
    myArray = (int*) malloc(10 * sizeof(int));
    myArray = string_to_array("1 2 3 4", myArray);

    printf("result: %d\n", myArray[0]);
    printf("result: %d\n", myArray[1]);
    return 0;
}

int* string_to_array(char * string, int * myArray){
    char delimiter[] = " ";
    char *ptr;
    char *ptr2;
    long ret;

    printf("string_to_array() was called.\n");
    printf("string is: %s\n", string);

    ptr = strtok(string, delimiter);            // here is the problem
    printf("strtok done; ptr: %s\n", ptr);

    int index = 0;
    while(ptr != NULL) {        
        ret = strtol(ptr, &ptr2, 10);
        printf("ret: %d\n", ret);
        myArray[index] = ret;
        ptr = strtok(NULL, delimiter);
        index++;
    }

    return myArray;
}
kame
  • 20,848
  • 33
  • 104
  • 159

2 Answers2

4

Since strtol is being use to parse the values, use ptr2 to iterate through string. After a conversion, ptr2 will point to the next character following the converted value. Use it as the starting point for the next conversion. More error checking can be done using strtol.

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

int* string_to_array(char * string, int * myArray);

int main(void) {
    int* myArray = 0;
    myArray = (int*) malloc(10 * sizeof(int));
    myArray = string_to_array("1 2 3 4", myArray);

    printf("result: %d\n", myArray[0]);
    printf("result: %d\n", myArray[1]);
    return 0;
}

int* string_to_array(char * string, int * myArray){
    char *ptr;
    char *ptr2;
    long ret;

    printf("string_to_array() was called.\n");
    printf("string is: %s\n", string);

    ptr = string;

    int index = 0;
    while(*ptr) {
        ret = strtol(ptr, &ptr2, 10);
        printf("ret: %ld\n", ret);
        myArray[index] = ret;
        ptr = ptr2;
        index++;
    }

    return myArray;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
4

As rafix07 wrote in his comment, you cannot use strtok with a string literal like "1 2 3 4". On my system I get a segmentation fault in strtok because of this.

To use an array instead of a string literal you can modify your program like this:

int main(void) {
    int* myArray = 0;
    char input[] = "1 2 3 4"; /* This initializes a writable array with the string literal */

    myArray = (int*) malloc(10 * sizeof(int));
    myArray = string_to_array(input, myArray); /* pass array instead of literal */

    printf("result: %d\n", myArray[0]);
    printf("result: %d\n", myArray[1]);
    return 0;
}

Not that strtok modifies your original data. If you don't want this you can make your function string_to_array work on a copy of the data. With this modification you can call it with a string literal as in your original main function.

int* string_to_array(char * string, int * myArray){
    char delimiter[] = " ";
    char *ptr;
    char *ptr2;
    long ret;
    char *data;
    data = strdup(string);

    /* FIXME error handling for strdup */

    printf("string_to_array() was called.\n");
    printf("string is: %s\n", string);

    ptr = strtok(data, delimiter);            // here is the problem
    printf("strtok done; ptr(%p): %s\n", ptr, ptr);

    int index = 0;
    while(ptr != NULL) {        
        ret = strtol(ptr, &ptr2, 10);
        printf("ret: %ld\n", ret);
        myArray[index] = ret;
        ptr = strtok(NULL, delimiter);
        index++;
    }

    free(data);
    return myArray;
}

Note that you don't need to return the input pointer myArray from string_to_array() and assign it to myArray in main() because the original array is modified.

Your function string_to_array() doesn't know how big you allocated your array. I suggest to pass the number of elements as a function parameter and check that you don't write behind the end of the array. You also don't return the number of elements in myArray you actually filled with data, so your main function might access uninitialized elements.

Bodo
  • 9,287
  • 1
  • 13
  • 29