在阅读这个问题之前,请注意我的问题是关于学校作业的。对于这个赋值,我们允许使用的唯一函数是malloc()。其他一切都必须在不使用其他库的情况下完成。
我正在调用一个函数ft_split_whitespaces。此函数以字符串作为输入,并将其“拆分”为单词。单词是分隔的空格、制表符和换行符。
#include <stdio.h>
char **ft_split_whitespaces(char *str);
int main(void)
{
char *str = "what is";
char **test = ft_split_whitespaces(str);
}
关于上面的例子,每个索引的结果应该是
test[0] = "what"
test[1] = "is"
test[2] = NULL
然而,我只能打印测试结果。所有其他索引都不会打印出来显示。下面是我认为应该打印函数结果的一些代码的示例。
int i = 0;
while(test[i] != NULL)
{
printf("%s", test[i]);
i++;
}
当运行这部分代码时,只会将测试打印到输出。我一直坐在这里试图调试我的代码几个小时。如果有人有空闲时间,并且不介意查看我的代码,我会非常感激的。我有一个感觉,这可能是一个问题,我如何使用malloc,但我仍然不知道它。
#include <stdlib.h>
#include <stdio.h>
int count_whitespaces(char *str)
{
int space_count;
int i;
space_count = 0;
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == 9 || str[i] == '\n')
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
space_count++;
}
i++;
}
return (space_count);
}
int check_whitespace(char c)
{
if (c == ' ' || c == 9 || c == '\n' || c == '\0')
{
return (1);
}
else
return(0);
}
int count_characters(char *str, int i)
{
int char_count;
char_count = 0;
while(str[i])
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
char_count++;
else
break;
i++;
}
return (char_count);
}
char **ft_split_whitespaces(char *str)
{
int i;
int j;
int k;
char **word;
int space;
i = 0;
j = 0;
k = 0;
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
j++;
word[j] = NULL;
j = 0
return word;
}
发布于 2016-08-21 02:52:16
你忘了重置k
了。在while
中的外部ft_split_whitespaces
循环应该如下所示
while (str[i] != '\0') {
if (check_whitespace(str[i]) == 1){
i++;
}
else {
if ((word[j] =
malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL){
return (NULL);
}
while (check_whitespace(str[i]) == 0) {
word[j][k] = str[i];
i++;
k++;
}
word[j][k] = '\0';
j++;
k = 0; // reset k
}
}
发布于 2016-08-21 02:30:14
因此,当我完成输入这个问题时,我实际上知道了我的代码到底出了什么问题(谢谢堆栈溢出!)我决定以任何方式发布它,因为我认为这对像我这样的新手来说可能是一次很好的学习体验。
这就是我的问题发生的地方。
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
我使用下面的代码将char **word置于while循环之外
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
然后在实际的while循环i中,malloc再次使用
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
在同一个变量上多次使用malloc会导致各种奇怪的问题。因此,在while循环中,我使用了一个新变量,声明为char *word。下面是具有正确代码的while循环的这一部分。
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
words = malloc(sizeof(char) * (count_characters(str, i) + 1));
k = 0;
while (check_whitespace(str[i]) == 0)
{
words[k] = str[i];
i++;
k++;
}
words[k] = '\0';
word[j] = words;
j++;
}
}
https://stackoverflow.com/questions/39060343
复制相似问题