为size_t和unsigned int之间的区别? [英] Difference between size_t and unsigned int?

查看:270
本文介绍了为size_t和unsigned int之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为size_t 。我已经搜索在互联网上到处都提到为size_t 是一个无符号的类型,这样,就可以重新present非负值。

我的第一个问题是,如果是用来重新present非负值,我们为什么不使用 unsigned int类型而不是为size_t ?

我的第二个问题是:为size_t unsigned int类型互换或不?如果没有,那么为什么呢?

和任何人都可以给我为size_t 的一个很好的例子,其短暂的工作?


解决方案

  

如果使用重新present非负值,所以我们为什么不使用 unsigned int类型而不是为size_t


由于 unsigned int类型不是唯一的无符号整数类型。 为size_t 可以是任何将unsigned char 无符号短 unsigned int类型无符号长无符号长长,视就执行。


  

第二个问题是,为size_t和unsinged INT可以互换与否,如果没有,为什么?


他们是不能互换的,对于上面的原因解释 ^ ^


  

和任何人都可以给我为size_t的一个很好的例子,其短暂的工作?


我不太让你的它短暂的工作的意思。它的工作原理是任何其他无符号类型(尤其是,像类型它typedeffed到)。我们鼓励您使用为size_t 当你描述一个物体的大小。尤其是,的sizeof 运营商和各类标准库函数,如的strlen(),返回为size_t

奖励:这里是一个很好的文章(文章删除,请使用<一个href=\"http://web.archive.org/web/20140828142605/http://www.$c$cproject.com/Articles/60082/About-size_t-and-ptrdiff_t\">wayback机快照)关于为size_t (以及与之密切相关的 ptrdiff_t的类型)。原因吧非常好,为什么你应该使用它。

I am so confused about size_t. I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent non-negative value.

My first question is that if it is uses to represent non negative values, why don't we use unsigned int instead of size_t?

My second question is: are size_t and unsigned int interchangeable or not? If not then why?

And can anyone give me a good example of size_t and its brief working?

解决方案

if it is use to represent non negative value so why we not using unsigned int instead of size_t

Because unsigned int is not the only unsigned integral type. size_t could be any of unsigned char, unsigned short, unsigned int, unsigned long or unsigned long long, depending on the implementation.

Second question is that size_t and unsinged int are interchangable or not and if not then why?

They aren't interchangeable, for the reason explained above ^^.

And can anyone give me a good example of size_t and its brief working ?

I don't quite get what you mean by "its brief working". It works like any other unsigned type (in particular, like the type it's typedeffed to). You are encouraged to use size_t when you are describing the size of an object. In particular, the sizeof operator and various standard library functions, such as strlen(), return size_t.

Bonus: here's a good article (article deleted, use wayback machine snapshot) about size_t (and the closely related ptrdiff_t type). It reasons very well why you should use it.

这篇关于为size_t和unsigned int之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆