指针/在C / C数组语法(字符** P,* P [N])+ [英] Pointer / Array syntax (char **p, *p[n]) in C/C++
问题描述
有关指针,我越来越混淆声明和函数参数上如果一个函数需要一个(*数组[n])参数时使用的char **或字符*或*阵列[N]等一样,我将它传递一个**类型?
For pointers, I'm getting confused with declarations and function parameters on when to use char ** or char * or *array[n], etc. Like if a function takes a (*array[n]) parameter, do I pass it a **type?
我尝试使用左右规则,知道是p是一个指针的指针为char(字符** P),p是n个指针数组(* P [N]),但有人说,* p [n]和** p为基本上相等。是真的吗?
I try using the Right-Left rule and know that p would be a pointer to a pointer to a char (char **p), and p is an array of n pointers (*p[n]), but someone said that *p[n] and **p are essentially equivalent. Is that true?
推荐答案
阅读Ç声明符(这是与*和[]变量的一部分)是相当细致入微。有一些网站与技巧:
Reading C declarators (that's the part of the variable with the * and []) is fairly nuanced. There are some websites with tips:
- http://www.antlr.org/wiki/display/CS652/How+To+Read+C+Declarations
- http://www.ericgiguere.com/articles/reading-c-declarations.html
A 的char **
是一个指针(可能是多个)指针(S)到(可能是多个)字符(S)。例如,它可能是一个指向一个字符串的指针,或指向串的指针数组。
A char**
is a pointer to (possible multiple) pointer(s) to (possibly multiple) char(s). For example, it might be a pointer to a string pointer, or a pointer to an array of string pointers.
A 的char * []
是指向字符数组。当你有一个函数,这个作为一个参数,C编译器使得它腐烂变成了的char **
。这只是发生在第一层...所以,采取了复杂的例子,的char * [4] []
变成的char *(*) 4]
。阅读上面这样你就可以明白这意味着什么赫克的链接。
A char*[]
is an array of pointers to char. When you have a function that takes this as a parameter, the C compiler makes it "decay" into a char**
. This only happens to the first layer... so, taking a complicated example, char*[4][]
becomes char*(*)[4]
. Read the links above so you can understand what the heck that means.
或者你也可以做一个(非常明智)的事情,让一堆类型定义的。我不这样做,但直到你擅长阅读说明符,这是一个好主意。
Or you can do a (very sensible) thing and make a bunch of typedefs. I don't do this, but until you're good at reading declarators, it's a good idea.
typedef char * stringp;
void func(stringp array[]) { ... }
static stringp FOUR_STRINGS[4] = { ... };
这篇关于指针/在C / C数组语法(字符** P,* P [N])+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!