比较char [2]和short [英] Compare char[2] with short

查看:121
本文介绍了比较char [2]和short的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在内存块中搜索char [2]数组的方法

" DA"使用指向短的指针。理想情况下我想写

类似于:


short * data = ...一些数据......;

int j = 0;

while(data [j]!= *((short *)" DA"))j ++;


但这不是不行。 char [2]显然有一个等价的16位

I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:

short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;

But this doesn''t work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?

推荐答案

spasmous写道:
spasmous wrote:

我需要一种方法来搜索内存块以获取char [2]数组

" ; DA"使用指向短的指针。理想情况下我想写

类似于:


short * data = ...一些数据......;

int j = 0;

while(data [j]!= *((short *)" DA"))j ++;


但这不是不行。 char [2]显然具有相当的16位
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:

short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;

But this doesn''t work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?



只需按字符比较。


while(data [j]!=''D''&& ; data [j + 1]!=''A''))j ++;


别忘了添加数组末尾的检查!


-

Ian Collins。

Just compare character by character.

while( data[j] != ''D'' && data[j+1] != ''A'') ) j++;

Don''t forget to add checking for the end of the array!

--
Ian Collins.


5月17日,1:13 * pm,Ian Collins< ian -n ... @ hotmail.comwrote:
On May 17, 1:13*pm, Ian Collins <ian-n...@hotmail.comwrote:

spasmouswrote:
spasmouswrote:

我需要一种方法来搜索通过char [2]数组的内存块

" DA"使用指向短的指针。理想情况下我想写

类似于:
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:


short * data = ... some data ...;

int j = 0;

while(data [j]!= *((short *)" DA"))j ++;
short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;


但这不起作用。 char [2]显然具有相当的16位
But this doesn''t work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?



只需按字符比较。


while(data [j]!=''D''&& ; data [j + 1]!=''A''))j ++;


别忘了添加数组末尾的检查!


Just compare character by character.

while( data[j] != ''D'' && data[j+1] != ''A'') ) j++;

Don''t forget to add checking for the end of the array!



也许我混淆了,但至少在我的平台上char是8位而

short是16位。因此,一个数据[j]与两个字符相同。

Maybe I''m mixed up, but at least on my platform char is 8-bit and
short is 16-bit. So one data[j] is the same as two chars.


在comp.lang.c中,spasmous写道:
In comp.lang.c, spasmous wrote:

我需要一种方法来搜索内存块以获取char [2]数组

DA使用指向短的指针。理想情况下我想写

类似于:


short * data = ...一些数据......;

int j = 0;

while(data [j]!= *((short *)" DA"))j ++;


但这不是不行。 char [2]显然具有相当的16位
I need a way to search through a block of memory for a char[2] array
"DA" using a pointer to a short. Ideally I would like to write
something like:

short *data = ... some data...;
int j = 0;
while( data[j] != *((short*) "DA") ) j++;

But this doesn''t work. The char[2] obviously has an equivalent 16-bit
value so how do I get that info in a simple way?



具体实施[1](但仍然是IIRC,标准C中的合法)方式

将是


while(data [j]!=''DA'')++ j;

[1] ISO C 9899-1999 6.4.4.4。第10点(语义)说包含多个字符的

整数字符常量的值......是实现定义的
。 ''DA''是一个常数。


-

Lew Pitcher


Master Code Code& JOAT-in-training |已注册的Linux用户#112576
http://pitcher.digitalfreehold.ca/ |可根据要求提供GPG公钥

---------- Slackware - 因为我知道我在做什么。 ------

An implementation specific[1] (but still, IIRC, legal in standard C) way
would be to

while (data[j] != ''DA'') ++j;
[1] ISO C 9899-1999 6.4.4.4. Point 10 (Semantics) says "The value of an
integer character constant containing more than one character ... is
implementation-defined." ''DA'' is such a constant.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I''m doing. ------


这篇关于比较char [2]和short的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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