在一个变量中允许有多少个指针(*)?
让我们考虑下面的例子。
int a = 10;
int *p = &a;
类似地,我们可以有
int **q = &p;
int ***r = &q;
等等。
例如,
int ****************zz;
在一个变量中允许有多少个指针(*)?
让我们考虑下面的例子。
int a = 10;
int *p = &a;
类似地,我们可以有
int **q = &p;
int ***r = &q;
等等。
例如,
int ****************zz;
当前回答
2004 MISRA C标准的17.5规则禁止超过2级的指针间接。
其他回答
每个c++开发人员都应该听说过著名的三星程序员。
而且似乎真的有某种神奇的“指针障碍”必须被掩盖。
C2引用:
Three Star Programmer A rating system for C-programmers. The more indirect your pointers are (i.e. the more "*" before your variables), the higher your reputation will be. No-star C-programmers are virtually non-existent, as virtually all non-trivial programs require use of pointers. Most are one-star programmers. In the old times (well, I'm young, so these look like old times to me at least), one would occasionally find a piece of code done by a three-star programmer and shiver with awe. Some people even claimed they'd seen three-star code with function pointers involved, on more than one level of indirection. Sounded as real as UFOs to me.
2004 MISRA C标准的17.5规则禁止超过2级的指针间接。
从理论上讲:
你可以有任意多的间接层次。
实际:
当然,任何消耗内存的操作都不能是无限的,主机环境中的可用资源会有限制。因此,实际上实现所能支持的内容是有最大限制的,实现应该适当地记录它。因此,在所有这些工件中,标准没有指定最大限度,但它指定了下限。
参考资料如下:
5.2.4.1翻译限制:
- 12个指针、数组和函数声明符(任意组合) 声明中的算术、结构、联合或空类型。
这指定了每个实现必须支持的下限。注意,在一个脚注中,标准进一步说:
18)实现应该尽可能避免强加固定的翻译限制。
实际上,C程序通常使用无限指针间接。一个或两个静态级别是常见的。三重间接是很少见的。但是无限是很常见的。
无限指针间接是在结构的帮助下实现的,当然,不是通过直接的声明器,这是不可能的。需要一个结构体,这样你就可以在不同的层次上在这个结构中包含其他数据。
struct list { struct list *next; ... };
现在你可以有list->next->next->next->…->next。这实际上只是多个指针指向:*(*(..(*(*(*list).next).next).next…).next).next).next。当。next是这个结构的第一个元素时,它基本上就是一个noop,所以我们可以把它想象成***. ***ptr。
这实际上没有任何限制,因为链接可以通过循环来遍历,而不是像这样一个巨大的表达式,而且,结构可以很容易地变成圆形。
因此,换句话说,链表可能是添加另一层间接来解决问题的最终示例,因为您对每个推操作都是动态地进行操作的。:)
这取决于你存储指针的位置。如果它们是堆叠的,极限就会很低。如果你把它存储在堆里,你的极限会非常非常高。
看看这个程序:
#include <iostream>
const int CBlockSize = 1048576;
int main()
{
int number = 0;
int** ptr = new int*[CBlockSize];
ptr[0] = &number;
for (int i = 1; i < CBlockSize; ++i)
ptr[i] = reinterpret_cast<int *> (&ptr[i - 1]);
for (int i = CBlockSize-1; i >= 0; --i)
std::cout << i << " " << (int)ptr[i] << "->" << *ptr[i] << std::endl;
return 0;
}
它创建了1M个指针,并在显示什么指向什么时很容易注意到链到第一个变量数。
顺便说一句。它使用92K的RAM,所以想象一下你能走多深。