char 指针的动态数组

发布于 2024-12-19 02:19:55 字数 376 浏览 6 评论 0原文

我试图定义一个包含指向字符串的字符指针的动态数组。字符串可以是任意长度,所以我使用了 char 指针。我想在每次需要存储更多值时动态调整数组的大小。下面的代码给了我分段错误。我做得好吗?

int main() {
    char **input=NULL;
    char *buffer;

    int i=0;

    do {
        input = (char **)realloc(input, (i+1) * sizeof(char *));

        scanf("%s", &buffer);
        strcpy(input[i++],buffer);

    } while(strlen(buffer)!=0);


}

I am trying to define a dynamic array containing char pointers that point to strings. The string can be of any length, so i used char pointers. I want to dynamically resize the array each time I need to store more values. The following code gives me segmentation fault. Am I doing it well?

int main() {
    char **input=NULL;
    char *buffer;

    int i=0;

    do {
        input = (char **)realloc(input, (i+1) * sizeof(char *));

        scanf("%s", &buffer);
        strcpy(input[i++],buffer);

    } while(strlen(buffer)!=0);


}

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

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

发布评论

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

评论(5

情绪 2024-12-26 02:19:56

问题是您将 buffer 定义为指针,但没有将其指向可以保存扫描字符串的对象,因此 scanf 在未分配的内存中写入,这实际上是坏的。您也没有分配input[i],因此strcpy也会失败。您可能想更改为使用 strdup 代替:

input[i++] = strdup(buffer);

另外一些挑剔:扫描字符串时,您不需要字符串缓冲区上的 &,因此重做像这样:

scanf("%s", buffer);

并且您不应该强制转换 realloc (或 malloc )的结果:

input = realloc(input, (i+1) * sizeof(char *));

The problem is that you define buffer as a pointer, but you do not point it to something that can hold the scanned string, so scanf writes out in unallocated memory which is really bad. You also do not allocate input[i] so strcpy will also fail. You might want to change that to use strdup instead:

input[i++] = strdup(buffer);

Another couple of nitpicks: When scanning for a string, you do not need the & on the string buffer, so redo like this:

scanf("%s", buffer);

And you should not cast the result of realloc (or malloc either for that matter):

input = realloc(input, (i+1) * sizeof(char *));
不交电费瞎发啥光 2024-12-26 02:19:56

除了其他答案中已经列出的问题之外,循环永远不会终止,因为:

scanf("%s", buffer);

在读取至少一个字符(不包括行尾字符)之前不会返回。

In addition to the issues already listed in the other answers the loop will never terminate as:

scanf("%s", buffer);

will not return until it reads at least one character, excluding the end of line character.

滥情稳全场 2024-12-26 02:19:56

您的缓冲区和输入[i]未分配。

Your buffer and your input[i] are not allocated.

说谎友 2024-12-26 02:19:55

您的代码中有多个问题。除非您修复所有这些问题,否则它不会起作用

  • 您没有为缓冲区分配内存。 Scanf 会写入稀薄的空气,如果不是下一点可以说是更严重的
  • 您应该将 buffer 传递给 scanf,而不是 &buffer< /strong>
  • 您没有为 input[i] 分配内存。你只是分配
    输入的内存。

There are multiple issues in your code. It won't work until you fix all of them

  • You aren't allocating memory for buffer. Scanf would write into thin air, if it weren't for the next point which is arguably more serious
  • You should pass buffer to scanf, not &buffer
  • You aren't allocating memory for input[i]. You are only allocating
    memory for input.
多情出卖 2024-12-26 02:19:55

缓冲区未初始化。此外, &buffer 是一个指向指针的指针,而不是您可能想要的字符数组。

buffer is uninitialized. Besides &buffer is a pointer to a pointer, not to a character array, as you probably intend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文