三连星:有什么字符*之间的差值(* ARR)[]和char *** ARR(C语言)? [英] Triple stars: What's the difference between char* (*arr)[] and char*** arr (in C)?

查看:190
本文介绍了三连星:有什么字符*之间的差值(* ARR)[]和char *** ARR(C语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有字符*数组,我想通过在这个功能修改,所以我在一个指针传递给一个字符数组*。也就是说,我想传递一个指针为char * ARR []。两者有什么区别呢?

Basically, I have an array of char* that I want to pass and modify in this function, so I pass in a pointer to an array of char*. That is, I want to pass a pointer to char* arr[]. What is the difference between the two?

推荐答案

像往常一样, http://cdecl.org 是你的朋友:

As always, http://cdecl.org is your friend:

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

These are not the same. For a start, the first is an incomplete type (in order to use a pointer to an array, the compiler needs to know the array size).

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

Your aim isn't entirely clear. I'm guessing that really all you want to do is modify the underlying data in your array of char *. If so, then you can just pass a pointer to the first element:

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);

如果您真的想一个指针传递给一个数组,那么像这样的工作:

If you really want to pass a pointer to an array, then something like this would work:

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常见问题的专门章节:的 http://c-faq.com/aryptr/index.html

For more info on the important difference between arrays and pointers, see the dedicated chapter of the C FAQ: http://c-faq.com/aryptr/index.html.

这篇关于三连星:有什么字符*之间的差值(* ARR)[]和char *** ARR(C语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆