c - my program doesn't print array?

My program has the goal of parsing the string "Ge34eks-f10or-Gee59ks". It does this by using strtok to split on delimiter '-' into three tokens Ge34eks, f10or, and Gee59ks. Each token is passed into num_extraction func with the goal of getting the numbers from each token: 34, 10, 59. They will be stored in an array with a zero in between each number : [34, 0, 10, 0, 59]. When attempting to print array nothing appears. Note: The while loop handles the second and third token, so if it is commented out, the first token number,34,is correctly printed.

Specific issue: Nothing is printed? And no error is given

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int* num_extraction(char* a, int* arr, int* index) 
{
    char* p = a;
    //int i = 0;
    while (*p)
    {  // While there are more characters to process...
        if (isdigit(*p) || ((*p == '-' || *p == '+') && isdigit(*(p + 1))))
        {
            // Found a number
            arr[*index] = strtol(p, &p, 10);  // Read number
            //printf("%d\n", val);          // and print it.
            *(index)++;
        }
        else
        {
            // Otherwise, move on to the next character.
            p++;
        }

    }
    return arr;

}

int main()
{
    char str[] = "Ge34eks-f10or-Gee59ks";
    int array[100] = { 0 };
    //int* p = array;
    int count = 0;
    int* q = &count;
 
 
    // Returns first token
    char *token = strtok(str, "-");
    num_extraction(token, array, q);
    count += 2;
   
    // Keep printing tokens while one of the
    // delimiters present in str[].
    
    while (token != NULL) // problem starts here if loop is commented out first token number is printed
    {
        //printf("%s\n", token);
        token = strtok(NULL, "-");
        num_extraction(token, array, q);
        count += 2;
    }
    
    for(int i = 0; i < count; i++)
    {
        printf("%d\n", array[i]);
    }
    //printf("%d\n", count);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.