问题描述
#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_spliter 函数内打印列表,但我不能在它之外(在主函数中)打印它。
一般来说,我想知道如何打印从函数返回的二维动态数组。
解决方法
致命错误:
- 您必须为
words
的每个元素分配,而不是仅分配 1 个字节。 -
words[c_words] = (char*) malloc(strlen(token)*sizeof(char));
不好,因为它没有为终止空字符分配空间。 - 您必须返回数组才能让
main()
打印数组。 - 您在
NULL
函数中使用main()
作为结束标记,因此my_spliter
函数应该添加它。
警告:
- 他们说you shouldn't cast the result of
malloc()
in C。 - 您应该在托管环境中使用标准的
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]);
}
}
省略了分配和释放的错误检查。请注意,您不需要在现代操作系统的程序结束时释放数据,因为操作系统会释放它们。 (参考:c - What REALLY happens when you don't free after malloc? - Stack Overflow)