我正在尝试使用 strtok 和动态数组创建拆分函数。 但是,我不知道哪里出了问题:没有信息性错误消息。 它确实说段错误,但我不明白堆是如何损坏的或发生的任何原因。 有人愿意向我解释哪里出了问题以及如何正确地做吗?
编辑 11:16 CST 代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **toArray(char **array, char str[], char sep[], int *count);
char** my_split(const char* str, char delim, int* size);
int main(int argc, char* argv[]) {
char* test = "Hello there lol";
int *count = 0;
char **array = malloc(sizeof(char*) * 5);
toArray(array, test, " ", count);
printf("Count: %d\n", *count);
int array_i;
for (array_i = 0; array_i < *count; array_i++) {
printf("array %d: %s\n", array_i, array[array_i]);
free(array[array_i]);
}
free(array);
return 1;
}
char **toArray(char **array, char str[], char sep[], int *count) {
char *temp = str;
temp = strtok(temp, sep);
array[0] = temp;
*count = 1;
while ((temp = strtok(NULL, sep)) != NULL ) {
array[(*count)++] = temp;
}
return array;
}
最佳答案
编译器消息是我们的 friend 。我简单地使用它们来追踪您的问题。 尝试以下操作,并将已完成的与您已完成的进行比较。特别注意指针变量的声明和使用...:)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char **toArray(char **array, char str[], char sep[], int *count);
int main(int argc, char* argv[]) {
char test[] = "Hello there lol";
int count = 0;
char **array = malloc((sizeof(char*) * 5) +1); //added "+ 1" here, read why
toArray(array, test, " ", &count); //in comment below
printf("Count: %d\n", count);
int array_i;
for (array_i = 0; array_i < count; array_i++) {
printf("array %d: %s\n", array_i, array[array_i]);
//free(array[array_i]);
}
getchar();
free(array);
return 1;
}
char **toArray(char **array, char str[], char sep[], int *count) {
char *temp = str;
temp = strtok(temp, sep);
array[0] = temp;
*count = 1;
while ((temp = strtok(NULL, sep)) != NULL) {
array[(*count)++] = temp;
}
return array;
}
[编辑] 示例输出:
还有。 char **array = malloc(sizeof(char*) * 5);
行,需要是
char **array = malloc(sizeof(char*) * 5 + 1);
因为“hello”实际上是 5 个字符加上一个 NULL 字符,'\0'
。
一些经验法则 C string(s)。
1) 在使用 malloc 或 calloc 时,不要忘记为 '\0'
留出空间。
`char *buf1;` //buffer needed to manipulate buf2
`char buf2[]="someString";`
`buf1 = malloc(strlen(buf2)+1);` or `buf1 = malloc(sizeof(buf2));`
(note:, no '+1'. see '4)' below. )
2) 在使用前清除(初始化)新分配的变量。例如:
memset(buf, 0, strlen("someString")+1); //preferred, all bytes are zeroed
或
buf[0]=0; //useful, but use with care (only first byte is zeroed.)
3) 完成后释放所有动态分配的内存。例如:
free(buf);
4) 使用 strlen()
函数或 sizeof()
宏。 (都在 [mc]alloc()
中流行使用)
给定:
char *buf1 = "你好";//6 个字符 |H|e|l|l|o|\0|
char buf2[] = "你好";//6 个字符 |H|e|l|l|o|\0|
char buf3[5]="你好";//5 个字符 |H|e|l|l|o|
char buf4[5]="Hel";//4 个字符 |H|e|l|\0| |
char buf5[5]="Hello";//应该得到编译错误,太多的初始化器
比较 strlen() - sizeof()
结果:
strlen(buf1);//->5
(要求在 malloc 中 +1 以便新变量要求保存“Hello\0”)
大小(buf1);//->4
(返回 sizof (char *),而不是字符串中的 # 个字符)
strlen(buf2);//->5
(需要在 malloc 中为新变量请求 +1,你要持有“Hello\0”)
大小(buf2);//->6
(计算所有字符,包括'\0')
strlen(buf3);//->
(错误:字符串参数中缺少终止 NULL)
大小(buf3);//->5
(计算所有字符,但此字符串中没有 '\0' - 错误!)
strlen(buf4);//->3
(计算字符,但不计算'\0')
大小(buf4);//->5
(计算所有分配的空间,包括'\0')
关于c - 尝试使用 strtok 和 malloc 但一直出错,我看不到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341684/