使用字符作为数组下标 [英] using character as array subscript

查看:68
本文介绍了使用字符作为数组下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




使用char索引数组的最佳语法是什么。


////// /////////////////////////////

例如


int data [256];


data [''a''] = 1;

data [''b''] = 1; < br $>
///////////////////////////////////


gcc正在抱怨这种语法,所以我在

字符文字上使用静态强制转换。有没有更好的方法呢?


谢谢,

伊万

解决方案

"伊凡" < iv ** @ novickmail.com写信息

新闻:e1 **************************** ****** @ i18g2000 prn.googlegroups.com ...





什么是最好的语法使用char来索引数组。


/////////////////////////// ////////

例如


int data [256];


data [ ''a''] = 1;

数据[''b''] = 1;

////////////// /////////////////////


gcc正在抱怨这种语法,所以我在
字符文字。有一个更好的方法吗?



MSVC ++ 2008 express并没有抱怨并且编译代码很好,甚至没有警告。只要您的本机类型是无符号的8位字节,它就是明确定义的行为。


在我的系统上如果我

std :: cout<< typeid('''')。name()< " \ n";

我得到的输出

char


不是unsigned char。如果你想要使用大于127的字符作为一个字节,那么这可能会为你产生一些未定义的行为,他们

可能会显示为负数。


Jim Langston写道:


" Ivan" < iv ** @ novickmail.com写信息

新闻:e1 **************************** ****** @ i18g2000 prn.googlegroups.com ...


>

什么是最好的语法使用char来索引数组。

///////////////////////////////// //
例如

int data [256];

数据[''a''] = 1;
数据[''b ''] = 1;
///////////////////////////////////

gcc抱怨这个语法,所以我在
字符文字上使用静态强制转换。有一个更好的方法吗?



MSVC ++ 2008 express并没有抱怨并编译好代码,甚至没有警告。只要您的本机类型是无符号的8位字节,它就是明确定义的行为。


在我的系统上如果我

std :: cout<< typeid('''')。name()< " \ n";

我得到的输出

char


不是unsigned char。如果你想要使用大于127的字符作为一个字节,那么这可能会为你产生一些未定义的行为,他们

可能会显示为负数。



它是否定义明确?我认为这取决于使用的字符编码

,例如ASCII与EBCDIC。或者标准现在是否实际指定了

字符编码?


-

Daniel Pitts的'科技博客:< http ://virtualinfinity.net/wordpress/>


2008年6月16日星期一18:53:05 -0700,Daniel Pitts

< ne ****************** @ virtualinfinity.netwrote in comp.lang.c ++:


Jim Langston写道:


" Ivan" < iv ** @ novickmail.com写信息

新闻:e1 **************************** ****** @ i18g2000 prn.googlegroups.com ...





什么是最好的语法使用char来索引数组。


/////////////////////////// ////////

例如


int data [256];


data [ ''a''] = 1;

数据[''b''] = 1;

////////////// /////////////////////


gcc正在抱怨这种语法,所以我在
字符文字。有一个更好的方法吗?



MSVC ++ 2008 express并没有抱怨并且编译代码很好,甚至没有警告。只要您的本机类型是无符号的8位字节,它就是明确定义的行为。


在我的系统上如果我

std :: cout<< typeid('''')。name()< " \ n";

我得到的输出

char


不是unsigned char。如果你想要使用大于127的字符作为一个字节,那么这可能会为你产生一些未定义的行为,他们

可能会显示为负数。



它是否定义明确?我认为这取决于使用的字符编码

,例如ASCII与EBCDIC。或者标准现在实际上指定了

char编码?



否,标准不指定执行字符集。或者来源

字符集,就此而言。这正是为什么它更容易使用实际字符,而不是在特定字符集中的数值

值。

事实上,OP的代码很可能是初学者作业的一部分

来生成某些输入数据中的字符直方图。


无论字符集如何,这都保证为无符号int的

最低半字节生成正确的十六进制数字字符:


char hex [] =" 0123456789ABCDEF" ;;


char hex_digit(unsigned int x)

{

return hex [x &安培;如果你将数组的定义更改为:


char hex [17] = {48,48,/ * ... * / 69,70,0};


....然后你得到完全相同的数组和结果在ASCII

实现,以及任何其他执行字符集的乱码。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// c -faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu .edu / ~ajo / docs / FAQ-acllc.html


Hi,

What is the best syntax to use a char to index into an array.

///////////////////////////////////
For example

int data[256];

data[''a''] = 1;
data[''b''] = 1;
///////////////////////////////////

gcc is complaining about this syntax, so i am using static cast on the
character literal. Is there a better way to do this?

Thanks,
Ivan

解决方案

"Ivan" <iv**@novickmail.comwrote in message
news:e1**********************************@i18g2000 prn.googlegroups.com...

Hi,

What is the best syntax to use a char to index into an array.

///////////////////////////////////
For example

int data[256];

data[''a''] = 1;
data[''b''] = 1;
///////////////////////////////////

gcc is complaining about this syntax, so i am using static cast on the
character literal. Is there a better way to do this?

MSVC++ 2008 express isn''t complaining and compiles that code fine, not even
a warning. It is well defined behavior as long as the type of your native
char is unsigned 8 bit byte.

On my system if I
std::cout << typeid(''a'').name() < "\n";
I get the output of
char

Not unsigned char. That may produce some undefined behavior for you if you
attempt to work with characters that would be above 127 as a byte, they
might show up negative.


Jim Langston wrote:

"Ivan" <iv**@novickmail.comwrote in message
news:e1**********************************@i18g2000 prn.googlegroups.com...

>Hi,

What is the best syntax to use a char to index into an array.

///////////////////////////////////
For example

int data[256];

data[''a''] = 1;
data[''b''] = 1;
///////////////////////////////////

gcc is complaining about this syntax, so i am using static cast on the
character literal. Is there a better way to do this?


MSVC++ 2008 express isn''t complaining and compiles that code fine, not even
a warning. It is well defined behavior as long as the type of your native
char is unsigned 8 bit byte.

On my system if I
std::cout << typeid(''a'').name() < "\n";
I get the output of
char

Not unsigned char. That may produce some undefined behavior for you if you
attempt to work with characters that would be above 127 as a byte, they
might show up negative.

Is it well defined? I thought it would depend on the character encoding
used, such as ASCII vs EBCDIC. Or does the standard actually specify
char encoding now?

--
Daniel Pitts'' Tech Blog: <http://virtualinfinity.net/wordpress/>


On Mon, 16 Jun 2008 18:53:05 -0700, Daniel Pitts
<ne******************@virtualinfinity.netwrote in comp.lang.c++:

Jim Langston wrote:

"Ivan" <iv**@novickmail.comwrote in message
news:e1**********************************@i18g2000 prn.googlegroups.com...

Hi,

What is the best syntax to use a char to index into an array.

///////////////////////////////////
For example

int data[256];

data[''a''] = 1;
data[''b''] = 1;
///////////////////////////////////

gcc is complaining about this syntax, so i am using static cast on the
character literal. Is there a better way to do this?

MSVC++ 2008 express isn''t complaining and compiles that code fine, not even
a warning. It is well defined behavior as long as the type of your native
char is unsigned 8 bit byte.

On my system if I
std::cout << typeid(''a'').name() < "\n";
I get the output of
char

Not unsigned char. That may produce some undefined behavior for you if you
attempt to work with characters that would be above 127 as a byte, they
might show up negative.

Is it well defined? I thought it would depend on the character encoding
used, such as ASCII vs EBCDIC. Or does the standard actually specify
char encoding now?

No, the standard does not specify execution character set. Or source
character set, for that matter. That''s exactly why it is more
portable to use the actual characters, rather than their numerical
value in a particular character set.

In fact, the OP''s code could well be part of a beginner''s assignment
to generate a histogram of characters in some input data.

This is guaranteed to produce the correct hex digit character for the
lowest nibble of an unsigned int regardless of the character set:

char hex[] = "0123456789ABCDEF";

char hex_digit(unsigned int x)
{
return hex [x & 0xf];
}

....if you change the definition of the array to:

char hex [17] = { 48, 48, /*... */ 69, 70, 0 };

....then you get exactly the same array and result on an ASCII
implementation, and gibberish on any other execution character set.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于使用字符作为数组下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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