简体   繁体   English

如何返回二维数组

[英]How can I return a two dimensional array

char **splitWords(){
    
    FILE *inp;
    char **arr[1000][30];
    int i = 0;
    int word_count = 0;
    char c;
    int char_count = 0;

    inp = fopen("C:\\Users\\ksmcn\\Desktop\\free.txt", "r");
    
    while ((c = fgetc(inp)) != EOF) {
        if (c == ' ' || c == '\n') {
          //  printf("\n");

            arr[word_count][char_count] = '\0'; //Terminate the string
            char_count = 0; //Reset the counter.
            word_count++;
        }
        else {
            **arr[word_count][char_count] = c; 
        //  printf("%c",arr[word_count][char_count]);

             if (char_count < 99)
                 char_count++;
             else
                 char_count = 0;
        }   
    }
    fclose(inp);
    return **arr;
}

int main(int argc, char *argv[]) {
    
    char **array[1000][30];   
    char array = splitWords();
   
    return 0;
}

I want to return a two dimensional array using c programming.我想使用 c 编程返回一个二维数组。 My splitWords function stores words as elements of an array and there is no issue about it.我的splitWords函数将单词存储为数组的元素,这没有问题。 However I couldn't return my array to work in main function.但是我无法让我的数组在主函数中工作。 Please help!请帮忙!

char **arr[1000][30];

You're declaring a 2D array of pointers to char pointers?您要声明指向char指针的二维指针数组? I think you mean char arr[1000][30] .我认为您的意思是char arr[1000][30] If you really do mean what this is, you are only initializing the space for a pointer, not the actual content of the pointer.如果您真的是这个意思,那么您只是在初始化指针的空间,而不是指针的实际内容。

 inp = fopen("C:\\Users\\ksmcn\\Desktop\\free.txt", "r");

You should add a sanity check in case the file couldn't be opened.如果文件无法打开,您应该添加完整性检查。 In other words, check if inp is NULL .换句话说,检查inp是否为NULL

 arr[word_count][char_count] = '\0'; //Terminate the string

This will work if you declared arr as char[][] , but you declared it as **char[][] .如果您将arr声明为char[][] ,但您将其声明为**char[][] ] ,这将起作用。 See above.看上面。

 **arr[word_count][char_count] = c;

If you're declaring arr as char[][] , you need arr[word_count][char_count] .如果您将arr声明为char[][] ,则需要arr[word_count][char_count] With your current declaration, this would work.使用您当前的声明,这将起作用。

 if (char_count < 99)

Your array is only big enough to store 30 char s.您的数组仅足以存储 30 个char Also, where's the check for if word_count exceeds 1000?另外,如果word_count超过 1000,检查在哪里? Not to mention, if you reset char_count , then it will overwrite existing values!更不用说,如果您重置char_count ,那么它将覆盖现有值!

 return **arr;

Dereferencing an uninitialized pointer is UB if I recall correctly.如果我没记错的话,取消引用未初始化的指针是 UB。 If your array were declared as char[][] , you'd need to do return arr .如果您的数组被声明为char[][] ,则需要return arr

 char **array[1000][30];

Why are you declaring something of type char **[][] when the function returns char ** ?当函数返回char **时,为什么要声明char **[][]类型的东西?

 char array = splitWords();

Why are you redefining array as type char ?为什么将array重新定义为char类型? This won't work!这行不通! Not to mention (once again) splitWords() returns char ** , not char .更不用说(再次) splitWords()返回char ** ,而不是char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM