字符到整数指针的转换 [英] char to integer pointer conversion

查看:135
本文介绍了字符到整数指针的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main()
{
 char *s="ABCDEFG";
 clrscr();

 int *ptr=(int *)s;
 printf("%c %d\n",*(ptr+1),*(ptr+1));          //OP :- C 17475
 printf("%c %d\n",*(s+1),*(s+1));              //OP :- B 66

 getch();
}

我知道整数指针增加2个字节,而char指针增加1个字节. 此处,当int指针增加1时,仅打印C(仅考虑第一个字节).是因为我们有%c说明符?

I know that integer pointer increments by 2 bytes whereas char pointer increments by 1 byte. Here when int pointer increments by 1, only C is printed (only first byte considered). Is it because we have %c specifier ?

此外,我不明白如何将17475打印为输出.在第二种情况下,66是B的ASCII值.

Also, I am not able to understand how 17475 is printed as output. In second case 66 is ASCII value of B.

有人可以帮助我吗?

推荐答案

首先请注意您的代码具有未定义的行为,这一点很重要.这意味着我们不能仅参考C标准就生成的输出说任何话.各个系统的输出可能/将有所不同,某些系统甚至可能无法执行代码.

To start with it is important to notice that your code has undefined behavior. That means that we can not say anything about the generated output solely by referring to the C standard. The output may/will differ from system to system and some systems may not even be able to execute the code.

问题是您有许多char(char数组),但是您使用int指针进行访问.那是不允许的.

The problem is that you have a number of char (a char array) but you access it using an int pointer. That is not allowed.

但是,在特定的系统(您的系统)上,可以对输出为何看起来如此进行一些考虑. 但是请记住,它不是有效的C代码. 注意:正如Antti Haapala所指出的那样,代码语法是有效的-只是程序的行为未定义

However, on a specific system (your system) it is possible to do some consideration about why the output looks as it does. But do remember that it is not valid C code. note: As pointed out by Antti Haapala the code syntax is valid - it's just the behavior of the program which is undefined

字符串(又名char数组)将放置在内存中的某个位置,例如:

The string (aka char array) will be placed somewhere in memory like:

Address |    Bin    | Hex | Dec | Ascii char
--------------------------------------------
 base   | 0100 0001 |  41 | 65  | A
 base+1 | 0100 0010 |  42 | 66  | B
 base+2 | 0100 0011 |  43 | 67  | C
 base+3 | 0100 0100 |  44 | 68  | D
 base+4 | 0100 0101 |  45 | 69  | E
 and so on

请注意,内存中保存了二进制值. Hex,Dec,Ascii列只是相同二进制值的人为"视图.

Notice that the memory holds binary values. The Hex, Dec, Ascii columns are just a "human" view of the same binary value.

您的指针s具有值base,即它指向保存值0100 0001的存储位置(又名A).

Your pointer s has the value base, i.e. it points to the memory location that holds the value 0100 0001 (aka A).

然后,使ptr也指向base.

打印(即printf("%c %d\n",*(ptr+1),*(ptr+1));)时,ptr+1将指向一个取决于整数大小的位置(每个系统不同).由于您的int大小为2,因此ptr+1是位置base + 2,即0100 0011(又名C).

When printing (i.e. printf("%c %d\n",*(ptr+1),*(ptr+1));), the ptr+1 will point to a location that depends on the size of integers (which differs from system to system). Since you have size of int being 2, ptr+1 is the location base + 2, i.e. 0100 0011 (aka C).

所以这句话的第一部分:

So the first part of this statement:

printf("%c %d\n",*(ptr+1),*(ptr+1));
        ^^       ^^^^^^^^

打印一个C,即在base+2位置的字符.

prints a C, i.e. the char at location base+2.

第二部分

printf("%c %d\n",*(ptr+1),*(ptr+1));
           ^^             ^^^^^^^^

打印位于base+2的整数值. (请注意-这是非法的,因为那里没有整数,但让我们暂时忘记它).

prints the integer value located at base+2. (note - which is illegal as there is no integer there but let's forget that for a moment).

在您的情况下,int是两个字节.因此,使用的字节将是C(十六进制:0x43)和D(十六进制:0x44).打印的值取决于系统的字节序.

In your case int is two bytes. So the used bytes will be the C (hex: 0x43) and the D (hex: 0x44). The value printed will depend on the endianness of your system.

Big endian(MSB在前)会给出:

Big endian (MSB first) will give:

0x4344 which is 17220 in decimal

小尾数(LSB在前)会给出:

Little endian (LSB first) will give:

0x4443 which is 17475 in decimal

因此,从这个角度看,您的系统似乎是低端的.

So from this it seems your system is little endian.

如您所见,很多东西都是系统依赖性的,从C标准的角度来看,这是不可能的.

As you can see a lot of this stuff is very system dependant and from a C standard point of view it is impossible to tell what the out will be.

这篇关于字符到整数指针的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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