简体   繁体   English

C 编程打印从 function 返回的二维动态数组

[英]C Programming Print 2d dynamic Array Returned from function

I have the code below:我有以下代码:

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

char** my_spliter(char* text){
    char* token;
    char** words;
    int c_words = 0;

    words = (char*) malloc(sizeof(char));
    token = strtok(text, ".");
    while( token != NULL ) {
        if(strlen(token)>1){
             words[c_words] = (char*) malloc(strlen(token)*sizeof(char));
             strcpy(words[c_words], token);
             c_words++;
         }
         token = strtok(NULL, ".");
    }

    for(int i=0; i<c_words; i++)
         printf("'%s' ", words[i]); //This prints the words successfully
}

void main(){
    char *my_text;
    char **list;
    
    m_text = (char*) malloc(250*sizeof(char));
    
    strcpy(my_text, ".test..tes.tested...t");
    list = my_spliter(my_text);

    printf("%s\n", list[0]); //This gives me an error
    
    size_t i;
    for (i = 0; list[i] != NULL; i++){
        printf("%s\n", list[i]); //This also gives me an error
    }
    
}

I can print the list inside the my_spliter function as mentioned in the comment, but I can't print it outside of it (in main function).如评论中所述,我可以在 my_spliter function 中打印列表,但我不能在它之外打印它(在主函数中)。

Generally, I want to know how to print 2D dynamic array returned from a function.一般来说,我想知道如何打印从 function 返回的二维动态数组。

Fatal errors:致命错误:

  • You have to allocate for each elements of words instead of allocating only 1 byte.您必须为words的每个元素分配而不是仅分配 1 个字节。
  • words[c_words] = (char*) malloc(strlen(token)*sizeof(char)); is bad because it don't allocate space for terminating null-characters.不好,因为它没有为终止空字符分配空间。
  • You have to return the array to let main() print the array.您必须返回数组才能让main()打印数组。
  • You are using NULL as end mark in the main() function, so the my_spliter function should add that.您在main() function 中使用NULL作为结束标记,因此my_spliter function 应该添加它。

Warnings:警告:

  • They say you shouldn't cast the result of malloc() in C .他们说你不应该在 C 中转换malloc()的结果
  • You should use standard int main(void) in hosted environment instead of void main() , which is illegal in C89 and implementation-defined in C99 or later, unless you have some special reason to use non-standard signature.您应该在托管环境中使用标准int main(void)而不是void main() ,这在 C89 中是非法的,并且在 C99 或更高版本中是实现定义的,除非您有特殊原因使用非标准签名。

Fixed code:固定代码:

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

char** my_spliter(char* text){
    char* token;
    char** words;
    int c_words = 0;

    words = malloc(sizeof(*word)); // fix allocation size
    token = strtok(text, ".");
    while( token != NULL ) {
        if(strlen(token)>1){
             words = realloc(words, sizeof(*words) * (c_words + 2)); // 1 for current element, 1 for NULL
             words[c_words] = malloc((strlen(token)+1)*sizeof(char)); // fix allocation size
             strcpy(words[c_words], token);
             c_words++;
         }
         token = strtok(NULL, ".");
    }
    words[c_words] = NULL; // add NULL

    for(int i=0; i<c_words; i++)
         printf("'%s' ", words[i]); //This prints the words successfully

    return words; // return the array
}

int main(){ // use standard type
    char *my_text;
    char **list;
    
    m_text = (char*) malloc(250*sizeof(char));
    
    strcpy(my_text, ".test..tes.tested...t");
    list = my_spliter(my_text);

    printf("%s\n", list[0]);
    
    size_t i;
    for (i = 0; list[i] != NULL; i++){
        printf("%s\n", list[i]);
    }
    
}

Error checking of allocations and freeing are omitted.分配和释放的错误检查被省略。 Note that you won't need to free data at the end of program on modern OS because the OS will free them.请注意,您不需要在现代操作系统上的程序结束时释放数据,因为操作系统会释放它们。 (reference: c - What REALLY happens when you don't free after malloc? - Stack Overflow ) (参考: c - 当你在 malloc 之后没有释放时,真正会发生什么? - 堆栈内存溢出

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

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