gpt4 book ai didi

c - 遍历指向 char 指针数组的指针

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:22 25 4
gpt4 key购买 nike

假设我有以下代码:

char *array[] = {"one", "two", "three"};
char *(*arrayPtr)[] = &array;

如何遍历数组?我试过这样做但不起作用:

for(int i = 0; i < sizeof(array)/sizeof(array[0]); i++) {
printf("%s\n", (*arrayPtr + i));
}

最佳答案

您的方案缺少推导。 *arrayPtr + i 是数组第 i 个元素的地址。这意味着它是一个 char**。您至少需要取消引用:

printf("%s\n", *(*arrayPtr + i));

但是,那不是有效的 C,因为您在定义指针时省略了数组大小。我希望这不是您编写的实际代码。

另请注意,您可以将下标运算符用作 Blagovest Buyukliev指出,但要厌倦运算符的优先级。它是 (*arrayPtr)[i] 而不是 *arrayPtr[i]

关于c - 遍历指向 char 指针数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43198876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com