Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程打印函数返回的2d动态数组_C_Dynamic Arrays - Fatal编程技术网

C编程打印函数返回的2d动态数组

C编程打印函数返回的2d动态数组,c,dynamic-arrays,C,Dynamic Arrays,我的代码如下: #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, ".&q

我的代码如下:

#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
    }
    
}
#包括
#包括
#包括
字符**my_拆分器(字符*文本){
字符*令牌;
字符**字;
int c_words=0;
单词=(char*)malloc(sizeof(char));
令牌=strtok(文本“.”);
while(令牌!=NULL){
如果(strlen(令牌)>1){
单词[c_words]=(char*)malloc(strlen(token)*sizeof(char));
strcpy(单词[c_单词],标记);
c_words++;
}
令牌=strtok(空,“.”);
}
对于(int i=0;i致命错误:

  • 您必须为
    单词的每个元素分配,而不是只分配1个字节
  • words[c_words]=(char*)malloc(strlen(token)*sizeof(char));
    不好,因为它没有为终止空字符分配空间
  • 必须返回数组才能让
    main()
    打印数组
  • 您正在使用
    NULL
    作为
    main()
    函数中的结束标记,因此
    my\u spliter
    函数应该添加该标记
警告:

  • 他们说
  • 您应该在托管环境中使用标准的
    int main(void)
    ,而不是
    void main()
    ,这在C89和C99或更高版本中定义的实现中是非法的,除非您有特殊原因使用非标准签名
固定代码:

#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]);
    }
    
}
#包括
#包括
#包括
字符**my_拆分器(字符*文本){
字符*令牌;
字符**字;
int c_words=0;
words=malloc(sizeof(*word));//固定分配大小
令牌=strtok(文本“.”);
while(令牌!=NULL){
如果(strlen(令牌)>1){
words=realloc(words,sizeof(*words)*(c_words+2));//1表示当前元素,1表示NULL
words[c_words]=malloc((strlen(token)+1)*sizeof(char));//固定分配大小
strcpy(单词[c_单词],标记);
c_words++;
}
令牌=strtok(空,“.”);
}
words[c_words]=NULL;//添加NULL
对于(int i=0;i