C 打印字符串数组会产生错误的输出
我试图解决一个挑战,但我不知道我的代码出了什么问题 挑战是:C 打印字符串数组会产生错误的输出,c,malloc,C,Malloc,我试图解决一个挑战,但我不知道我的代码出了什么问题 挑战是: 创建将字符串拆分为单词的函数 分隔符是空格、制表符和换行符 此函数返回一个数组,其中每个框包含由单词表示的字符串地址。此数组的最后一个元素应等于0,以强调数组的结尾 数组中不能有任何空字符串。得出必要的结论。 无法修改给定的字符串 注意:唯一允许的函数是malloc() 错误/问题: 我面对这个问题,我试图解决它,但我无法确定是什么错了。 我创建了一个名为split_whitespaces()的函数来完成这项工作。 当我在spli
- 创建将字符串拆分为单词的函数
- 分隔符是空格、制表符和换行符
- 此函数返回一个数组,其中每个框包含由单词表示的字符串地址。此数组的最后一个元素应等于0,以强调数组的结尾
- 数组中不能有任何空字符串。得出必要的结论。 无法修改给定的字符串
- 注意:唯一允许的函数是
malloc()
split_whitespaces()
的函数来完成这项工作。
当我在split_whitespaces
函数中打印字符串数组时,我得到以下输出:
Inside the function:
arr_str[0] = This
arr_str[1] = is
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
Inside the main function:
arr_str[0] = @X@?~
arr_str[1] = `X@?~
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
当我在main
函数中打印字符串数组时,我得到以下输出:
Inside the function:
arr_str[0] = This
arr_str[1] = is
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
Inside the main function:
arr_str[0] = @X@?~
arr_str[1] = `X@?~
arr_str[2] = just
arr_str[3] = a
arr_str[4] = test!
我创建了一个函数word\u count
来计算输入字符串中的单词数,这样我就可以使用malloc和word\u count+1
(空指针)来分配内存
int字数(char*str){
int i;
int w_计数;
int状态;
i=0;
w_计数=0;
状态=0;
while(str[i]){
if(!iswhitespace(str[i])){
如果(!状态)
w_count++;
状态=1;
i++;
}否则{
状态=0;
i++;
}
}
返回(w_计数);
}
另一个名为strdup\w
的函数模拟strdup
的行为,但只针对单个单词:
char*strdup\w(char*str,int*index){
字符*字;
内伦;
int i;
i=*指数;
len=0;
while(str[i]&&!iswhitespace(str[i]))
len++,i++;;
word=(char*)malloc(len+1);
如果(!word)
返回(空);
i=0;
而(str[*索引]){
if(!iswhitespace(str[*index])){
单词[i++]=str[*索引];
(*索引)+;
}否则
打破
}
字[len]='\0';
返回(字);
}
以下是我的完整代码:
#包括
#包括
#包括
char**split_空格(char*str);
char*strdup_w(char*str,int*index);
整数字计数(字符*str);
int是空格(charc);
内部主(空){
char*str=“这只是一个测试!”;
字符**arr_str;
int i;
i=0;
arr_str=split_空格(str);
printf(“\n在函数外:\n”);
while(arr_str[i]){
printf(“arr_str[%d]=%s\n”,i,arr_str[i]);
i++;
}
返回(0);
}
char**split_空格(char*str){
字符**arr_str;
int i;
智力词;
int w_i;
i=0;
w_i=0;
单词=单词数(str);
arr_str=(char**)malloc(words+1);
如果(!arr_str)
返回(空);
printf(“函数内部:\n”);
while(w_i
很抱歉,如果有什么问题,这是我第一次尝试寻求帮助。代码中存在多个问题:
arr_str=(char**)malloc(words+1)中的大小不正确代码>必须将元素数乘以元素大小:
arr_str = malloc(sizeof(*arr_str) * (words + 1));
- 使用后在
函数中释放数组是一种很好的方式main()
- 测试
是多余的:如果while(iswhitespace(str[i])&&str[i])
w\u计数计算正确,则测试
应该是不必要的。您应该使用str[i]
跳过空白,使用strspn()
跳过单词字符strcspn()
如果(!str[i++])中断代码>在循环中是完全冗余的:
已经过测试,并且不为空str[i]
while(str[i]&&!iswhitespace(str[i]))len++,i++代码>是不好的样式。如果循环体中有多个简单语句,请使用大括号
中的最后一个循环很复杂,您可以简单地使用strdup\w
memcpy(word,str+*index,len)*指数+=len代码>
#include <stdio.h>
#include <stdlib.h>
char **split_whitespaces(const char *str);
char *strdup_w(const char *str, int *index);
int word_count(const char *str);
int iswhitespace(char c);
int main(void) {
const char *str = "This is just a test!";
char **arr_str;
int i;
arr_str = split_whitespaces(str);
if (arr_str) {
printf("\nOutside the function:\n");
i = 0;
while (arr_str[i]) {
printf("arr_str[%d] = %s\n", i, arr_str[i]);
i++;
}
while (i --> 0) {
free(arr_str[i]);
}
free(arr_str);
}
return 0;
}
char **split_whitespaces(const char *str) {
char **arr_str;
int i;
int words;
int w_i;
i = 0;
w_i = 0;
words = word_count(str);
arr_str = malloc(sizeof(*arr_str) * (words + 1));
if (!arr_str)
return NULL;
printf("Inside the function:\n");
while (w_i < words) {
while (iswhitespace(str[i]))
i++;
arr_str[w_i] = strdup_w(str, &i);
if (!arr_str[w_i])
break;
printf("arr_str[%d] = %s\n", w_i, arr_str[w_i]);
w_i++;
}
arr_str[words] = NULL;
return arr_str;
}
char *strdup_w(const char *str, int *index) {
char *word;
int len;
int start;
int i;
i = *index;
start = i;
while (str[i] && !iswhitespace(str[i])) {
i++;
}
*index = i;
len = i - start;
word = malloc(len + 1);
if (!word)
return NULL;
i = 0;
while (i < len) {
word[i] = str[start + i];
i++;
}
word[i] = '\0';
return word;
}
int word_count(const char *str) {
int i;
int w_count;
int state;
i = 0;
w_count = 0;
state = 0;
while (str[i]) {
if (!iswhitespace(str[i])) {
if (!state)
w_count++;
state = 1;
} else {
state = 0;
}
i++;
}
return w_count;
}
int iswhitespace(char c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
#包括
#包括
字符**分割空白(const char*str);
char*strdup_w(常量char*str,int*index);
整数字计数(常量字符*str);
int是空格(charc);
内部主(空){
const char*str=“这只是一个测试!”;
字符**arr_str;
int i;
arr_str=split_空格(str);
如果(arr_str){
printf(“\n在函数外:\n”);
i=0;
while(arr_str[i]){
printf(“arr_str[%d]=%s\n”,i,arr_str[i]);
arr_str = malloc(sizeof(char *) * (words + 1));
arr_str = malloc(sizeof(struct string) * (words + 1));
arr_str = malloc(sizeof(*arr_str) * (words + 1));