C语言 C: 将 int 转换为 size_t

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5481067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:12:56  来源:igfitidea点击:

C: cast int to size_t

ccastingintc99size-t

提问by All Workers Are Essential

What is the proper way to convert/cast an intto a size_tin C99 on both 32bit and 64bit linux platforms?

在 32 位和 64 位 linux 平台上在 C99 中将an 转换/转换int为 a的正确方法是什么size_t

Example:

例子:

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];
}

回答by R.. GitHub STOP HELPING ICE

All arithmetic types convert implicitly in C. It's very rare that you need a cast - usually only when you want to convert down, reducing modulo 1 plus the max value of the smaller type, or when you need to force arithmetic into unsigned mode to use the properties of unsigned arithmetic.

所有算术类型都在 C 中隐式转换。很少需要强制转换 - 通常仅当您想向下转换、减少模 1 加上较小类型的最大值时,或者当您需要强制算术进入无符号模式以使用时无符号算术的性质。

Personally, I dislike seeing casts because:

就个人而言,我不喜欢看演员表,因为:

  1. They're ugly visual clutter, and
  2. They suggest to me that the person who wrote the code was getting warnings about types, and threw in casts to shut up the compiler without understanding the reason for the warnings.
  1. 它们是丑陋的视觉混乱,并且
  2. 他们向我暗示,编写代码的人收到了有关类型的警告,并在不了解警告原因的情况下投入了强制转换以关闭编译器。

Of course if you enable some ultra-picky warning levels, your implicit conversions might cause lots of warnings even when they're correct...

当然,如果您启用了一些非常挑剔的警告级别,即使它们是正确的,您的隐式转换也可能会导致大量警告......

回答by Stephen Canon

size_t key_index = (size_t)hash(key) % size;

is fine. You actually don't even need the cast:

很好。你实际上甚至不需要演员:

size_t key_index = hash(key) % size;

does the same thing.

做同样的事情。

回答by Tim

Aside from the casting issue (which you don't need as stated before), there is some more intricate things that might go wrong with the code.

除了转换问题(您不需要如前所述)之外,还有一些更复杂的事情可能会导致代码出错。

if hash()is supposed to return an index to an array, it should return a size_tas well. Since it doesn't, you might get weird effects when key_indexis larger than INT_MAX.

如果hash()应该返回一个数组的索引,它也应该返回一个size_t。由于它没有,当key_index大于时,您可能会得到奇怪的效果INT_MAX

I would say that size, hash(), key_indexshould all be of the same type, probably size_tto be sure, for example:

我会说size, hash(),key_index应该都是相同的类型,可能size_t是肯定的,例如:

size_t hash(void * key) {
    //...
}

int main (int argc, char * argv[]) {
    size_t size = 10;
    void * items[size];
    //...
    void * key = ...;

    size_t key_index = hash(key) % size;
    void * item = items[key_index];
}