数组查找[原因:为什么是“Hello World”; const char *?] [英] Array Lookup [Was: Why is "Hello World" const char* ?]

查看:101
本文介绍了数组查找[原因:为什么是“Hello World”; const char *?]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


感谢您对我签名/未签名字符的问题的回复。


问题是:我想要使用char数组进行ArrayLookup,所以

我需要将char转换为unsigned int。


但这不起作用!


如果我试试这个:


#include< stdio.h>

int main(){

char c = -1; // 129无符号和-1签名吧?

//将数据转换为0到255之间的数组查找

unsigned int arrayIndex =(unsigned int)c;

if(arrayIndex< 0)

printf(" ArrayIndex在转换为unsigned int后仍然为负数

%d \ n", arrayIndex);

else

printf("无符号表示对于ArrayIndex =%d \ n",arrayIndex);

}


输出为:ArrayIndex在转换为无符号后仍然为负值

int -1"


如何这可能吗?


我希望输出129.


谢谢!


Hans

Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn''t work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex);
}

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?

I would expect a output 129.

Thanks!

Hans

推荐答案

Hans写道:
Hans wrote:
你好,

感谢所有你的回答关于签名/未签名字符的问题。

问题是:我想使用char数组进行ArrayLookup,所以
我需要将char转换为unsigned int。

但这不起作用!

如果我试试这个:

#include< stdio.h> ;
int main(){
char c = -1; // 129无符号和-1签名对吗?


错误。


//将数据转换为0到255之间的数组Lookup
unsigned int arrayIndex =(unsigned int)c;



不需要铸造。


这相当于


unsigned int arrayIndex = numeric_limits< unsigned int> :: max();

或UINT_MAX of< climits> ;.


if(b) arrayIndex< 0)
printf(ArrayIndex在转换为unsigned int后仍然为负数%
%d \ n,arrayIndex);

printf("无符号表示)可以用于ArrayIndex =%d \ n",arrayIndex);
}

输出为:ArrayIndex在转换为无符号后仍为负值
int -1

这怎么可能?
Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn''t work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
Wrong.

// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;

The casting is not needed.

Also this is equivalent to

unsigned int arrayIndex = numeric_limits<unsigned int>::max();
or UINT_MAX of <climits>.


if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex);
}

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?



%d用于int,%u用于unsigned int。

我刚注意到你是交叉发布到clc,通常是一件坏事。

因为C和C ++是不同的语言。

无论如何你的程序修复并且是有效的C99和C ++ 03:

#include< stdio.h>

#include< limits.h>


int main()

{

char c = -1;


unsigned int arrayIndex = c ;


if(arrayIndex< 0)

printf(转换为无符号后ArrayIndex仍为负数

int%u \ n,arrayIndex);


else

printf("无符号表示适用于

ArrayIndex =%u \ n \ nn",arrayIndex);

printf(UINT_MAX is%u\ n,UINT_MAX);

}


-

Ioannis Vranos

http://www23.brinkster.com/ noicys


Hans写道:
Hans wrote:
输出结果为:< ArrayIndex在转换为无符号后仍然为负/> int -1"

这怎么可能?
The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?



但是,原始的错误代码没有打印上面的内容。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


Hans写道:
您好,

感谢您对我的签名/未签名字符问题的所有回复。

问题是:我想将char数组用于ArrayLookup,所以
我需要将char转换为unsigned int。

但这并不是'工作了!

如果我试试这个:

#include< stdio.h>
int main(){
char c = - 1; // 129无符号和-1签名对吗?


错误。 c是-1或UCHAR_MAX(对于CHAR_BIT == 8:255)

//将其转换为0到255之间的值,用于数组Lookup


错误。您可以从以下获取UCHAR_MAX或UINT_MAX

unsigned int arrayIndex =(unsigned int)c;


可能你想要... =(unsigned char)c;

否则,演员阵容是不必要的。

if(arrayIndex< 0)
printf(ArrayIndex在转换为unsigned int后仍然为负数%
%d \ n,arrayIndex);
%u;你在说谎类型,所以printf()会弄错。 else
printf("无符号表示对于ArrayIndex =%d \ n",arrayIndex);
Dito。输出结果为:转换为无符号后,ArrayIndex仍为负数
int -1"


你撒谎。上面的输出可能是

无符号表示可以用于ArrayIndex = -1 \ n

其他任何东西都是不可能的。
怎么样这可能吗?


不行。
我希望输出129.


我期望CHAR_BIT == 8 255或-1和固定的
printf()格式字符串255或UINT_MAX可能是

2 ** 16-1或2 ** 32-1 (65,XXX或4,XXX,XXX,XXX)。


另见Ioannis Vranos的回复。


请决定是否以clc或clc ++发表。

F''up2 clc

谢谢!
Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn''t work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
Wrong. c is either -1 or UCHAR_MAX (for CHAR_BIT==8: 255)
// convert it to a value between 0 - 255 for array Lookup
Wrong. You either get UCHAR_MAX or UINT_MAX from the following
unsigned int arrayIndex = (unsigned int) c;
Probably, you want to have ... = (unsigned char) c;
Otherwise, the cast is unnecessary.
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex); %u; you are lying about the type, so printf() will get it wrong. else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex); Dito. }

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"
You are lying. The output of the above probably is
"Unsigned representation is OK for ArrayIndex=-1\n"
Anything else is not possible.
How is this possible?
It ain''t.
I would expect a output 129.
I would expect for CHAR_BIT==8 either 255 or -1 and for the fixed
printf() format string either 255 or UINT_MAX which is probably either
2**16-1 or 2**32-1 (65,XXX or 4,XXX,XXX,XXX).

See also the replies of Ioannis Vranos.

Please get decided whether to post in clc or clc++.
F''up2 clc
Thanks!




你是欢迎。

干杯

Michael

-

电子邮件:我的是/ at / gmx /点/地址。



You''re welcome.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于数组查找[原因:为什么是“Hello World”; const char *?]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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