【发布时间】:2023-11-22 16:50:01
【问题描述】:
在 32 位和 64 位 Linux 平台上,在 C99 中将 int 转换/转换为 size_t 的正确方法是什么?
例子:
int hash(void * key) {
//...
}
int main (int argc, char * argv[]) {
size_t size = 10;
void * items[size];
//...
void * key = ...;
// Is this the right way to convert the returned int from the hash function
// to a size_t?
size_t key_index = (size_t)hash(key) % size;
void * item = items[key_index];
}
【问题讨论】:
-
我不明白为什么它不起作用。
-
普通的普通演员
(size_t)有什么问题?它在某种程度上不“正确”吗? -
是的,没错,虽然我认为它是隐式转换的,所以你并不真的需要
(size_t)。 -
根本不投射有什么问题?