本文介绍了#define ArrayLength(x)(sizeof(x)/sizeof(*(x)))是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这行到底是什么意思?
很明显,定义是什么,但我不明白为什么在分母处传递x的指针:
It is clear what define is, but I don't understand why is passing the pointer of x at the denominator:
#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))
谢谢
推荐答案
分母
sizeof(*(x))
是数组中第一个元素的长度(以字节为单位).变量x
是数组类型,它 衰减 指向数组开头的指针.星号(*
)是解引用运算符,因此*(x)
表示" x
"指向的数据.
is the length of the first element in the array in bytes. The variable x
is of an array type, and it decays to a pointer, pointing to the start of the array. The asterisk (*
) is the dereference operator, so *(x)
means "the data pointed to by x
".
sizeof(x)
将sizeof
运算符应用于数组类型.这样就给出了整个数组的长度(以字节为单位).
applies the sizeof
operator to an array type. This gives the length of the entire array in bytes.
#define ArrayLength(x) (sizeof(x)/sizeof(x[0]))
这也许更容易阅读.
这篇关于#define ArrayLength(x)(sizeof(x)/sizeof(*(x)))是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!