我正在尝试创建函数,它将字符串作为参数,将其划分为字符串,将空格分隔的单词,将其保存到数组并通过指针返回。尽管内存静态分配给数组,但是由于分段错误,程序崩溃了。这段代码有什么问题?
void separate_words(char* full_text, char *matrix[], int* how_many)
{
char tmp;
int actual_letter,which_letter=0;
for(actual_letter=0;actual_letter<strlen(full_text);actual_letter++)
{
if(full_text[actual_letter]!=32)
{
full_text[actual_letter];
matrix[*how_many][whitch_letter]=full_text[actual_letter];//here crashes
}
else
{
*how_many++;
which_letter=0;
}
}
//*how_many
}
/*...*/
char words[20][20];
char text[20];
int number_of_words=0;
separate_words(text,words,&number_of_words);
答案 0 :(得分:1)
当您使用strtok
#include <stdio.h>
#include <string.h>
void separate_words(char* full_text, char *matrix[], int* how_many)
{
char *pch;
pch = strtok (full_text," \t\n");
int i = 0;
while (pch != NULL)
{
matrix[i++] = pch;
pch = strtok (NULL, " \t\n");
}
*how_many = i;
}
int main ()
{
char str[] = "apple banana orange pineapple";
char *matrix[100] = { NULL };
int how_many = 0;
separate_words(str, matrix, &how_many);
int i;
for( i = 0; i < how_many; i++ )
{
if( matrix[i] != NULL )
{
printf( "matrix[%d] = %s\n", i, matrix[i] );
}
}
return 0;
}
输出:
matrix[0] = apple
matrix[1] = banana
matrix[2] = orange
matrix[3] = pineapple
答案 1 :(得分:0)
将分隔符与strtok
一起用作函数的参数以将字符串分隔为标记通常很有用。在某些情况下,您还可以使用for
循环strtok
来整理代码。
由于分配给结果数组的指针数永远不会为负数,因此选择size_t
或unsigned
的数据类型可以帮助编译器指出该变量后来是否使用不当
最后,如果使用一个静态大小的指针数组来保存每个标记的指针,那么测试指定的指针数是很重要的,以防止写入数组的末尾。 注意:如果您动态分配指针(使用malloc
或calloc
),则可以在达到初始限制时调用realloc
,然后根据需要调用#include <stdio.h>
#include <string.h>
#define MAXP 100
void separate_words (char *a[], char *s, char *delim, size_t *n);
int main (void)
{
char str[] = "apple banana orange pineapple";
char *delim = " \t\n";
char *matrix[MAXP] = { NULL };
size_t how_many = 0;
size_t i;
separate_words (matrix, str, delim, &how_many);
for (i = 0; i < how_many; i++)
printf ("matrix[%zu] = %s\n", i, matrix[i]);
return 0;
}
/* separate 's' into tokens based on delimiters provided in 'delim'
* with pointers to each token saved in 'a' with 'n' updated to hold
* the number of pointers contained in 'a'.
*/
void separate_words (char *a[], char *s, char *delim, size_t *n)
{
*n = 0;
char *p = strtok (s, delim);
for (; p; p = strtok (NULL, delim)) {
a[(*n)++] = p;
if (*n == MAXP) {
fprintf (stderr, "warning: pointer limit reached.\n");
return;
}
}
}
。
将各种部分放在一起,另一种选择是:
$ ./bin/strtok_static
matrix[0] = apple
matrix[1] = banana
matrix[2] = orange
matrix[3] = pineapple
<强>输出强>
#include <stdio.h>
#include <string.h>
#define MAXP 100
size_t separate_words (char *a[], char *s, char *delim);
int main (void)
{
char str[] = "apple banana orange pineapple";
char *delim = " \t\n";
char *matrix[MAXP] = { NULL };
size_t how_many = 0;
size_t i;
how_many = separate_words (matrix, str, delim);
for (i = 0; i < how_many; i++)
printf ("matrix[%zu] = %s\n", i, matrix[i]);
return 0;
}
/* separate 's' into tokens based on delimiters provided in 'delim'
* with pointers to each token saved in 'a'. The number of tokens is
* returned.
*/
size_t separate_words (char *a[], char *s, char *delim)
{
size_t n = 0;
char *p = strtok (s, delim);
for (; p; p = strtok (NULL, delim)) {
a[n++] = p;
if (n == MAXP) {
fprintf (stderr, "warning: pointer limit reached.\n");
break;
}
}
return n;
}
使用退货
此外,您可以使用函数return返回字符串中的标记数。这消除了将指针变量作为参数传递给函数的需要:
url(r'^guitar/$', include('guitar.urls')),