这个 c strdup 代码有什么问题?

发布于 2024-09-01 09:43:05 字数 334 浏览 6 评论 0原文

考虑这段代码:

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, *tmp;
short iter = 0;

tmp = ptr1;
while (iter < 2)
{
   tmp = strdup(strs[iter]);
   tmp = ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

我希望输出“string1\nstring2\n”,但 str1 和 str2 保持为空。我做错了什么?

Consider this code:

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, *tmp;
short iter = 0;

tmp = ptr1;
while (iter < 2)
{
   tmp = strdup(strs[iter]);
   tmp = ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

I want this to output "string1\nstring2\n" however str1 and str2 remain null. What am I doing wrong?

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

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

发布评论

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

评论(6

比忠 2024-09-08 09:43:05

没有名为 str1str2 的变量,因此我假设您指的是 ptr1ptr2

您从未向这些变量分配任何内容,因此它们没有理由更改其原始值。我认为这就是您的意图:

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, **tmp;
short iter = 0;

tmp = &ptr1;
while (iter < 2)
{
   *tmp = strdup(strs[iter]);
   tmp = &ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

然而,这是一段相当奇怪的代码。你到底想达到什么目的?可能有更优雅的解决方案。

There are no variables called str1 and str2, so I'll assume you meant ptr1 and ptr2.

You never assign anything to these variables, so there's no reason for them to change from their original values. I think this is what you intended:

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, **tmp;
short iter = 0;

tmp = &ptr1;
while (iter < 2)
{
   *tmp = strdup(strs[iter]);
   tmp = &ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

It's a fairly bizarre piece of code, however. What are you actually trying to achieve? There may be a more elegant solution.

违心° 2024-09-08 09:43:05

您没有为 ptr1ptr2 赋值。你可以做类似的事情:

while (iter < 2) {
    tmp = strdup(strs[iter]);
    if(iter == 0)
        ptr1 = tmp;
    else if(iter == 1)
        ptr2 = tmp;
    iter++;
}

或者更简单:

ptr1 = strdup(strs[0]);
ptr2 = strdup(strs[1]);

You're not assigning values to ptr1 and ptr2. You can do something like:

while (iter < 2) {
    tmp = strdup(strs[iter]);
    if(iter == 0)
        ptr1 = tmp;
    else if(iter == 1)
        ptr2 = tmp;
    iter++;
}

or even simpler:

ptr1 = strdup(strs[0]);
ptr2 = strdup(strs[1]);
南汐寒笙箫 2024-09-08 09:43:05

事实上,ptr1 和 ptr2 都没有更新。

从表面上看,您试图在 tmp 更新时更新 ptr1 和 ptr2 。为此, tmp 需要是一个双指针,并且您的代码需要看起来像......

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, **tmp;
short iter = 0;

tmp = &ptr1;
while (iter < 2)
{
   *tmp = strdup(strs[iter]);
   tmp = &ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

希望这会有所帮助。

As it is, neither ptr1 nor ptr2 are updated.

From the looks of it, you are trying to have ptr1 and ptr2 updated when tmp is updated. To do this, tmp would need to be a double pointer and your code would need to look like ...

char *strs[] = { "string1", "string2", NULL };
char *ptr1 = NULL, *ptr2 = NULL, **tmp;
short iter = 0;

tmp = &ptr1;
while (iter < 2)
{
   *tmp = strdup(strs[iter]);
   tmp = &ptr2;
   iter++;
}

printf("1: %s\n2: %s\n", ptr1, ptr2);

Hope this helps.

笑饮青盏花 2024-09-08 09:43:05

您永远不会为 ptr1ptr2 赋值。使用tmp = ptr1,您只需将ptr1的当前值(NULL)复制到tmp。但是,之后更改 tmp 不会影响 ptr1 的值。这就是指针的工作原理。

试试这个:

ptr1 = strdup(strs[0]);
ptr2 = strdup(strs[1]);

或者重新声明并使用 tmp 作为指向指针的指针,正如 @Marcelo Cantos 在他的答案中演示的那样。

You never assign a value to ptr1 and ptr2. With tmp = ptr1, you just copy the current value of ptr1 (which is NULL) over to tmp. However, changing tmp afterwards does not affect the value of ptr1. This is how it works with pointers.

Try this instead:

ptr1 = strdup(strs[0]);
ptr2 = strdup(strs[1]);

Or redeclare and use tmp as a pointer to pointer, as @Marcelo Cantos demonstrates in his answer.

陪我终i 2024-09-08 09:43:05

您声明 ptr1 和 ptr2 并将它们初始化为 null,但随后不设置它们的值。

You declare ptr1 and ptr2 and initialise them to null, but you don't subsequently set their values.

卖梦商人 2024-09-08 09:43:05

将 ptr1 和 ptr2 初始化为 NULL,并且从不更改它们。所以他们没有指出任何事情。

You initialize ptr1 and ptr2 to NULL, and never change them. So they point to nothing.

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