作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
给定一个用户自定义类型A
,和一个指针A* a
,*a
和有什么区别一个[0]
?
(虽然*(a+0)
/a[0]
被定义为等价的,但*a
/a[0]
,其中细微的差别可能会在某些情况下导致编译错误。)
最佳答案
如果 A
是不完整类型,则 *a
有效,但 a[0]
无效,in this example :
struct A;
void foo(A& r)
{
}
void bar(A* a)
{
foo(*a);
foo(a[0]); // error: invalid use of incomplete type ‘struct A’
}
那是因为 a[0]
等价于 *(a+0)
,但是你不能向指向不完整类型对象的指针添加一些东西(甚至零),因为指针算法需要知道大小。
关于c++ - `*a` 和 `a[0]` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487726/
我是一名优秀的程序员,十分优秀!