没有从c中的字符串数组函数获取输出

Not getting output from string array function in c

我在 C 中创建了一个拆分函数,以便在某些程序中使用它的 return 值。但是当我使用 printf 检查它的值时,我发现有一些错误,但我无法自己修复它们。我尽可能修复了大部分错误。

我写的代码是:

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

char **split(char *token, char *delimiter, int *a[], int *size_of_a) {
    int i = 0;
    char **final_result;
    char *str = strtok(token, delimiter);
    
    while (str != NULL) {
        *a[i] = strlen(str); //maybe one of the errors but I don't know how to fix it
        //even after removing a[i] by backslash and changing it in loop and main, there is still no output received in main
        getch();
        for (int j = 0; j < *a[i]; j++) { 
            final_result[i][j] = str[j];
        }
        str = strtok(NULL, delimiter);
        i++;
    }
    *size_of_a = i;
    return final_result;
}

int main() {
    char *parameter_1;
    char *parameter_2;
    int *size_1;
    int size_2;
    
    printf("Enter the token: ");
    scanf("%s", &parameter_1);
    printf("\nEnter the delimiter: ");
    scanf("%s", &parameter_2);
    
    char **result_2 = split(parameter_1, parameter_2, &size_1, &size_2);
    
    printf("\nThe result is:");
    for (int x = 0; x < size_2; x++) {
        printf('\n');
        for (int y = 0; y < size_1[x]; y++) {
            printf("%c", result_2[x][y]);
        }
    }
    getch();
    return 0;
}

如何修复输出错误?

代码中存在多个问题:

  • 您没有为指针数组分配 space:final_result 未初始化,通过取消引用存储任何内容都具有未定义的行为,很可能是分段错误。

    您应该使用 strcpn()strspn() 来计算令牌的数量,为 NULL 终止符分配带有或不带有额外插槽的数组,并执行第二阶段拆分标记并存储指向数组的指针。您可能希望存储标记的副本以避免修改可能不变或超出范围的原始字符串。

  • printf('\n'); 无效:您必须传递字符串,而不是字符常量。

  • scanf("%s", &parameter_1); 也有未定义的行为:你传递一个指针的地址而不是指向 char.

    数组的指针

这是修改后的版本:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

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

#ifdef _MSC_VER
// define POSIX function strndup if not available
char *strndup(const char *s, size_t n) {
    size_t len;
    for (len = 0; len < n && s[len]; len++)
        continue;
    char *ptr = malloc(len + 1);
    if (ptr) {
        memcpy(ptr, s, len);
        ptr[len] = '[=10=]';
    }
    return ptr;
}
#endif

char **split(const char *str, const char *delimiters, int **a, int *size_of_a) {
    int i, count, len;
    char **final_result;
    const char *p;

    // phase 1: count the number of tokens
    p = str + strspn(str, delimiters);
    for (count = 0; *p; count++) {
        p += strcspn(p, delimiters);
        p += strspn(p, delimiters);
    }

    // phase 2: allocate the arrays
    final_result = calloc(sizeof(*final_result), count + 1);
    if (a) {
        *a = calloc(sizeof(**a), count);
    }
    if (size_of_a) {
        *size_of_a = count;
    }

    // phase 3: copy the tokens
    p = str;
    for (i = 0; i < count; i++) {
        p += strspn(p, delimiters);    // skip the delimiters
        len = strcspn(p, delimiters);  // count the token length
        if (a) {
            (*a)[i] = len;
        }
        final_result[i] = strndup(p, len); // duplicate the token
        p += len;
    }
    final_result[count] = 0;
    return final_result;
}

// read and discard the rest of the user input line
int flush_input(void) {
    int c;
    while ((c = getchar()) != EOF && c != '\n')
        continue;
    return c;
}

int main() {
    char buf[256];
    char delimiters[20];
    
    printf("Enter the string: ");
    if (scanf("%255[^\n]", buf) != 1)
        return 1;
    flush_input();
    printf("\nEnter the delimiters: ");
    if (scanf("%19[^\n]", delimiters) != 1)
        return 1;
    flush_input();
    
    int *sizes;
    int count;
    char **array = split(buf, delimiters, &sizes, &count);
    
    printf("\nThe result is:\n");
    for (int x = 0; x < count; x++) {
        for (int y = 0; y < sizes[x]; y++) {
            putchar(array[x][y]);
        }
        printf("\n");
    }
    getchar();
    return 0;
}