尝试从字符串中解析数字并存储在数组中,但经过多次迭代后数组似乎没有内容

Attempting to parse numbers from strings and store in array but array seems to have no contents after more than one iteration

我的程序的目标是解析字符串“Ge34eks-f10or-Gee59ks”。它通过使用 strtok 将分隔符“-”拆分为三个标记 Ge34eks、f10or 和 Gee59ks 来实现。每个标记都被传递到 num_extraction func 中,目的是从每个标记中获取数字:34、10、59。我想在每个标记的数字之间保留空格,因此它们将存储在一个数组中,其中一个为零每个数字之间:[34, 0, 10, 0, 59]。尝试打印数组时,什么也没有出现。注意:while 循环处理第二个和第三个标记,因此如果将其注释掉,则正确打印第一个标记编号 34。

具体问题:什么都不打印?并且没有报错 猜测:要么我的计数变量有问题,要么我的数组没有正确存储数字。

#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;
}

版本 2

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

char* char_extraction(char* a, char* arr, int* index) 
{
    char* p = a;
    
    for( int i = 0; i < strlen(p); i++)
    {
        printf("%s\n", "test"); // ISSUE: ONLY PRINTS TWICE?
        if(isalpha(p[i]))
        {
            
            arr[*index] = p[i];
            //printf("%c\n", arr[*index]);  
            *(index)++;
        }
    }    
    return arr;

}


int main()
{
    char str[] = "Ge34eks-f10or-Gee59ks";
    int array[100] = { 0 };
    char array1[100] = "";
    int count1 = 0;
    int* r = &count1;
 
 
    // Returns first token
    char *token = strtok(str, "-");
    
    char_extraction(token, array1, r); // called once 
  
    count1 += 2;
   
    // Keep printing tokens while one of the
    // delimiters present in str[].
    
    
  
    while ((token = strtok(NULL, "-")) != NULL) 
    {
        
        char_extraction(token, array1, r); // should be called twice
        count1 += 2;
    }
    
    
    for(int i = 0; i < count1; i++)
    {
        printf("%c\n", array1[i]);
    }
    
    //printf("%d\n", count);
    return 0;
}


看这里 - 查看内联评论

while (token != NULL) 
{
    // token not null here from last loop
    token = strtok(NULL, "-");
    // now token is null if last
    num_extraction(token, array, q);
    count += 2;
}

你需要做

// get next token then test if NULL
while ((token = strtok(NULL, "-")) != NULL) 
{
    num_extraction(token, array, q);
    count += 2;
}

// get next token then test if NULL
for(;;)
{
   token = strtok(NULL, "-");
    if(token == NULL) break;
    num_extraction(token, array, q);
    count += 2;
}

甚至

 while (token = strtok(NULL, "-")) 
    {
        //printf("%s\n", token);
        num_extraction(token, array, q);
        count += 2;
    }