问题描述
这个问题出去到C大师那里:
This question goes out to the C gurus out there:
在C中,可以声明指针如下:
In C, it is possible to declare a pointer as follows:
char (* p)[10];
..基本上指出该指针指向的10个字符阵列。有关声明这样的指针的妙处是,如果你尝试分配不同大小的数组p的指针,你会得到一个编译时错误。如果你尝试分配一个简单的字符指针p的值它也会给你一个编译时错误。我想这与海湾合作委员会,它似乎与ANSI,C89和C99的工作。
.. which basically states that this pointer points to an array of 10 chars. The neat thing about declaring a pointer like this is that you will get a compile time error if you try to assign a pointer of an array of different size to p. It will also give you a compile time error if you try to assign the value of a simple char pointer to p. I tried this with gcc and it seems to work with ANSI, C89 and C99.
在我看来像宣称这样的指针将是非常有用的 - 尤其是,一个指针传递给函数时。通常,人们会写这样一个函数的原型是这样的:
It looks to me like declaring a pointer like this would be very useful - particularly, when passing a pointer to a function. Usually, people would write the prototype of such a function like this:
void foo(char * p, int plen);
如果你期待一个特定大小的缓冲区,你会简单地测试PLEN的值。但是,你不能保证谁通过p来你的人会真的给你PLEN在缓冲区中的有效存储位置。你要相信,谁调用此函数的人做正确的事。另一方面:
If you were expecting a buffer of an specific size, you would simply test the value of plen. However, you cannot be guaranteed that the person who passes p to you will really give you plen valid memory locations in that buffer. You have to trust that the person who called this function is doing the right thing. On the other hand:
void foo(char (*p)[10]);
..将迫使调用者给你指定大小的缓冲区。
..would force the caller to give you a buffer of the specified size.
这似乎是非常有用的,但我从来没有见过任何code我曾经跑过声明这样的指针。
This seems very useful but I have never seen a pointer declared like this in any code I have ever ran across.
我的问题是:是否有任何理由,为什么人们不声明这样的指针?我不能看到一些明显的缺陷?
My question is: Is there any reason why people do not declare pointers like this? Am I not seeing some obvious pitfall?
推荐答案
我想补充到AndreyT的回答(万一有人绊倒在本页面寻找更多信息关于这个主题):
I would like to add to AndreyT's answer (in case anyone stumbles upon this page looking for more info on this topic):
当我开始使用这些声明多玩,我认识到,有(显然不是在C ++)与它们用C相关的主要障碍。这是相当普遍有一个情况下,你想给呼叫者一个const指针已写入缓冲。不幸的是,在宣布C.这样的指针换句话说,当这是不可能的,C标准(6.7.3 - 第8段)是相左的东西是这样的:
As I begin to play more with these declarations, I realize that there is major handicap associated with them in C (apparently not in C++). It is fairly common to have a situation where you would like to give a caller a const pointer to a buffer you have written into. Unfortunately, this is not possible when declaring a pointer like this in C. In other words, the C standard (6.7.3 - Paragraph 8) is at odds with something like this:
int array[9];
const int (* p2)[9] = &array; /* Not legal unless array is const as well */
这限制似乎并不在C ++ present,使得这种类型的声明更为有用。但在C的情况下,有必要时,你想要一个const指针固定大小的缓冲区(除非缓冲区本身被声明为const先跟)回落到一个普通指针声明。你可以找到在这个邮件线程的详细信息:链接文本
This constraint does not seem to be present in C++, making these type of declarations far more useful. But in the case of C, it is necessary to fall back to a regular pointer declaration whenever you want a const pointer to the fixed size buffer (unless the buffer itself was declared const to begin with). You can find more info in this mail thread: link text
这在我看来是一个严重的制约,这可能是主要的原因,人们通常不会在C另一个是事实,大多数人甚至不知道,你可以声明指针声明这样的指针1像这样的AndreyT指出。
This is a severe constraint in my opinion and it could be one of the main reasons why people do not usually declare pointers like this in C. The other being the fact that most people do not even know that you can declare a pointer like this as AndreyT has pointed out.
这篇关于C指针:指向固定大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!