数组索引类型 [英] array index type

查看:111
本文介绍了数组索引类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么类型可以/应该用作数组索引?


例如。我可以使用unsigned long吗?或者应该是size_t?


例如:


int sum(int * a,index_t n){

int s = 0;

index_t i;


for(i = 0; i< n; ++ i)s + = a [i ];

返回s;

}

what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a[i];
return s;
}

推荐答案

Szabolcs Nagy写道:
Szabolcs Nagy wrote:

什么类型可以/应该用作数组索引?
what type can/should be used as array index?



好​​的 - 前往沙坑。我可以看到圣战即将到来......

OK - head for the bunkers. I can see holy war coming...


eg。我可以使用unsigned long吗?或者应该是size_t?


例如:


int sum(int * a,index_t n){

int s = 0;

index_t i;


for(i = 0; i< n; ++ i)s + = a [i ];

返回s;

}
eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a[i];
return s;
}



我只会尝试回答可以 ;你的问题的一部分...

当我读到它时,C语言规范只声明

索引/下标必须是整数类型 - 大小类型,存在或

没有指定标志。


在此基础上,如果您的申请要求无签名长期

index / subscript,然后继续使用它。


我肯定有人会很快告诉你应该是什么类型

使用....

I''ll only try to answer the "can" part of your question...

As I read it, the C language specification states only that the
index/subscript must be of integer type - size of the type, presence or
absence of sign is not specified.

On that basis, if your application calls for unsigned long as
index/subscript, then go ahead and use it.

I''m sure someone will be along shortly to tell you what type should be
used....


Szabolcs Nagy写道:
Szabolcs Nagy wrote:

什么类型可以/应该用作数组指数?


例如。我可以使用unsigned long吗?或者应该是size_t?


例如:


int sum(int * a,index_t n){

int s = 0;

index_t i;


for(i = 0; i< n; ++ i)s + = a [i ];

返回s;

}
what type can/should be used as array index?

eg. can i use unsigned long? or should it be size_t?

example:

int sum(int *a, index_t n) {
int s = 0;
index_t i;

for (i = 0; i < n; ++i) s += a[i];
return s;
}



虽然无符号类型应该有效,但可能需要做因此,按照C标准,它们可能会产生比未签名的

类型更多的开销。 unsigned long可能比其他

的可能性引发更多的可移植性问题。

While unsigned types should work, and are probably required to do so by
the C standard, they may incur significantly more overhead than unsigned
types. unsigned long probably incurs more portability issues than other
possibilities.




Tim Prince写道:

Tim Prince wrote:

虽然无符号类型应该可以使用,并且可能需要按照C标准来执行此操作,但它们可能会产生比未签名更多的开销。

类型。
While unsigned types should work, and are probably required to do so by
the C standard, they may incur significantly more overhead than unsigned
types.



签名还是未签名?

为什么会有开销?


感谢您的回答

signed or unsigned?
why is there overhead?

thanks for the answer


这篇关于数组索引类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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