gpt4 book ai didi

c - 三颗星 : What's the difference between char* (*arr)[] and char*** arr (in C)?

转载 作者:行者123 更新时间:2023-12-04 12:34:08 24 4
gpt4 key购买 nike

基本上,我有一个 char* 数组,我想在这个函数中传递和修改它,所以我传入一个指向 char* 数组的指针。也就是说,我想传递一个指向 char* arr[] 的指针。两者有什么区别?

最佳答案

一如既往,http://cdecl.org是你的 friend :

  • char * (*arr)[] - “将 arr 声明为指向 char 指针数组的指针”
  • char *** arr - “将 arr 声明为指向 char 指针的指针”

这些是不一样的。首先,第一个是不完整类型(为了使用指向数组的指针,编译器需要知道数组大小)。

您的目标并不完全清楚。我猜想您真正想要做的就是修改 char * 数组中的基础数据。如果是这样,那么您可以只传递一个指向第一个元素的指针:

void my_func(char **pointers) { 
pointers[3] = NULL; // Modify an element in the array
}

char *array_of_pointers[10];

// The following two lines are equivalent
my_func(&array_of_pointers[0]);
my_func(array_of_pointers);

如果您真的想要传递一个指向数组的指针,那么像这样的东西会起作用:

void my_func(char *(*ptr)[10]) {
(*ptr)[3] = NULL; // Modify an element in the array
}

char *array_of_pointers[10];

// Note how this is different to either of the calls in the first example
my_func(&array_of_pointers);

有关数组和指针之间重要区别的更多信息,请参阅 C FAQ 的专门章节:http://c-faq.com/aryptr/index.html .

关于c - 三颗星 : What's the difference between char* (*arr)[] and char*** arr (in C)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183581/

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