关于char数组强制转换成short int 类型后,输出问题

snake406635029 2014-01-24 01:47:41

int _tmain(int argc, _TCHAR* argv[])
{
char data[200];
for(int i = 0; i < 200; i++ )
{
data[i] = i + 1;
}
short int *p;
short int j;
p = (short int*)data;
for(j = 0; j < 100; j++)
{
*(p++) &= 0xff;
}
for (j = 0; j < 100; j++)
{
printf("%-4d\n",data[j]);
}
return 0;
}


这个输出结果比较怪,求解释
...全文
748 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-01-24
  • 打赏
  • 举报
回复
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构…… 对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
vipcxj 2014-01-24
  • 打赏
  • 举报
回复
2L已经把我要说的都说了,你的char数组内存是连续的,因为short占的内存空间比char大,你将数组中的char元素强制转换为short,再写入新值,会覆盖数组其他元素,最糟糕的情况是溢出。 你应该这样

    for(j = 0; j < 100; j++)
    {
        short int p = data[j];
        p &= 0xff;
        data[j] = p; // 在short为2个字节,char为1个字节的情况下,高字节被清0了
    }
MARIOV 2014-01-24
  • 打赏
  • 举报
回复
字节大小不对
赵4老师 2014-01-24
  • 打赏
  • 举报
回复
常量也有类型! C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value
JiMoKuangXiangQu 2014-01-24
  • 打赏
  • 举报
回复
引用 楼主 snake406635029 的回复:

int _tmain(int argc, _TCHAR* argv[])
{
	char data[200];
	for(int i = 0; i < 200; i++ )
	{
		data[i] = i + 1;
	}
	short int *p;
	short int j;
	p = (short int*)data;
	for(j = 0; j < 100; j++)
	{
		*(p++) &= 0xff;
	}
	for (j = 0; j < 100; j++)
	{
		printf("%-4d\n",data[j]);
	}
	return 0;
}
这个输出结果比较怪,求解释
#include <stdio.h>
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	printf("sizeof(char) = %d, sizeof(short) = %d\n", sizeof(char), sizeof(short));
	system("pause");

	char data[200];
	for(int i = 0; i < 200; i++ )
	{
		data[i] = i + 1;
	}
	short int *p;
	short int j;
	p = (short int*)data;
	for(j = 0; j < 100; j++)
	{
		*(p++) &= 0xff; // 在short为2个字节,char为1个字节的情况下,高字节被清0了
	}
	for (j = 0; j < 100; j++)
	{
		printf("%-4d\n",data[j]);
	}
	system("pause");
	return 0;
}
derekrose 2014-01-24
  • 打赏
  • 举报
回复
debug的时候观察memory

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧