分解字符串并将其存储在数组中

发布于 2024-12-14 14:34:07 字数 498 浏览 2 评论 0原文

我想分解一个句子并将每个字符串存储在一个数组中。这是我的代码:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int i = 0;
    char* strArray[40];
    char* writablestring= "The C Programming Language";
    char *token = strtok(writablestring, " ");


    while(token != NULL)
    {
        strcpy(strArray[i], token);
        printf("[%s]\n", token);
        token = strtok(NULL, " ");
        i++;
    }
    return 0;
}

它一直给我分段错误,我无法弄清楚。我相信当我将令牌复制到我的数组时它会发生一些事情。

I want to break down a sentence and store each string in an array. Here is my code:

#include <stdio.h>
#include <string.h>

int main(void)
{
    int i = 0;
    char* strArray[40];
    char* writablestring= "The C Programming Language";
    char *token = strtok(writablestring, " ");


    while(token != NULL)
    {
        strcpy(strArray[i], token);
        printf("[%s]\n", token);
        token = strtok(NULL, " ");
        i++;
    }
    return 0;
}

It keeps giving me segmentation error and I cannot figure it out. I believe it has something to do when I copy the token to my array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-12-21 14:34:07

这是因为 writablestring 根本不可写。尝试写入字符串文字是未定义的行为,strtok 会写入它(没错,strtok 会修改其参数)。

要使其正常工作,请尝试:

char writablestring[] = "The C Programming Language";

还有一个 C 常见问题解答

另一个问题是您没有为字符指针数组分配内存(因此这些指针指向任何内容)。

char* strArray[40]; /* Array of 40 char pointers, pointing to nothing. */

也许试试这个?

/* Careful, strdup is nonstandard. */
strArray[i] = strdup(token);

/* Or this. */
strArray[i] = malloc(strlen(token) + 1);
strcpy(strArray[i], token);

It's because writablestring isn't writable at all. Attempting to write to a string literal is undefined behavior and strtok writes to it (that's right, strtok modifies its argument).

To make it work, try:

char writablestring[] = "The C Programming Language";

There's also a C FAQ.

Another problem is that you didn't allocate memory for your array of character pointers (so those pointers point to nothing).

char* strArray[40]; /* Array of 40 char pointers, pointing to nothing. */

Maybe try this ?

/* Careful, strdup is nonstandard. */
strArray[i] = strdup(token);

/* Or this. */
strArray[i] = malloc(strlen(token) + 1);
strcpy(strArray[i], token);
瞄了个咪的 2024-12-21 14:34:07

看看文档中的示例

char * strtok ( char * str, const char * delimiters );

...其中.. 。

str -
要截断的 C 字符串。该字符串的内容被修改并分解为更小的字符串(标记)。
或者,可以指定空指针,在这种情况下,函数将继续扫描先前成功调用函数结束的位置。

分隔符 - 包含分隔符的 C 字符串。
这些可能因一次调用而异。

返回值 -
指向字符串中找到的最后一个标记的指针。
如果没有剩余的令牌可供检索,则返回空指针。

您需要我可以修改第一个字符串,并且需要为输出分配内存,例如

int main(void)
{
    int i = 0;
    const int numOfStrings = 128;
    char* strArray[numOfStrings];
    char writablestring[]= "The C Programming Language";
    char *token = strtok(writablestring, " ");

    for( int j = 0; j < numOfStrings; j++ )
    {
        strArray[j] = new char[40];
    }

    while(token != NULL)
    {
        strcpy(strArray[i], token);
        printf("[%s]\n", token);
        token = strtok(NULL, " ");
        i++;
    }
    return 0;
}

Have a look at the example in the docs:

char * strtok ( char * str, const char * delimiters );

...where...

str -
C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.

delimiters - C string containing the delimiters.
These may vary from one call to another.

Return Value -
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.

You need that first string to me modifiable and you need to allocate memory for the outputs e.g.

int main(void)
{
    int i = 0;
    const int numOfStrings = 128;
    char* strArray[numOfStrings];
    char writablestring[]= "The C Programming Language";
    char *token = strtok(writablestring, " ");

    for( int j = 0; j < numOfStrings; j++ )
    {
        strArray[j] = new char[40];
    }

    while(token != NULL)
    {
        strcpy(strArray[i], token);
        printf("[%s]\n", token);
        token = strtok(NULL, " ");
        i++;
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文