简体   繁体   English

类型转换char指针到整数指针

[英]Type casting char pointer to integer pointer

So I saw a few example on how the endianness of an architecture could be found. 因此,我看到了一些有关如何找到体系结构字节序的示例。 Let's say we have an integer pointer that points to an int data type. 假设我们有一个指向int数据类型的整数指针。 And let's say the int value is 0x010A0B12. 假设int值为0x010A0B12。 In a little endian architecture, the least significant byte, ie, 12, will be stored in the lowest memory address, right? 在小端序架构中,最低有效字节(即12)将存储在最低的内存地址中,对吗? So the lowest byte in a 4-byte integer will be 12. 因此,4字节整数中的最低字节为12。

Now, on to the check. 现在,继续检查。 If we declare a char pointer p, and type cast the integer pointer to a char * and store it in p, and print the dereferenced value of p, we will get a clue on the endianness of the architecture. 如果我们声明一个char指针p,然后将整数指针类型转换为char *并将其存储在p中,并打印p的取消引用的值,我们将获得有关体系结构字节序的线索。 If it's 12, we're little endian; 如果是12,则我们为小端。 01 signifies big endian. 01表示大字节序。 This sounds really neat... 听起来真的很整洁...

int a = 0x010A0B12;
int *i = &a;
char *p = (char*)i; 
printf("%d",*p); // prints the decimal equivalent of 12h!

Couple of questions here, really. 确实有几个问题。 Since pointers are strongly typed, shouldn't a character pointer strictly point to a char data type? 由于指针是强类型的,因此字符指针不应该严格指向char数据类型吗? And what's up with printing with %d? 用%d打印怎么办? Shouldn't we rather print with %c, for character? 我们不应该用%c打印字符吗?

Since pointers are strongly typed, shouldn't a character pointer strictly point to a char data type? 由于指针是强类型的,因此字符指针不应该严格指向char数据类型吗?

C has a rule that any pointer can be safely converted to char* and to void* . C有一个规则,可以将任何指针安全地转换为char*void* Converting an int* to char* , therefore, is allowed, and it is also portable. 因此,可以将int*转换为char* ,并且它也是可移植的。 The pointer would be pointing to the initial byte of your int 's internal representation. 指针将指向int内部表示形式的起始字节。

Shouldn't we rather print with %c , for character? 我们不应该用%c打印字符吗?

Another thing is in play here: variable-length argument list of printf . 这里还有另外一件事: printf变长参数列表。 When you pass a char to an untyped parameter of printf , the default conversion applies: char gets converted to int . char传递给printf的无类型参数时,将应用默认转换: char被转换为int That is why %d format takes the number just fine, and prints it out as you expect. 这就是为什么%d格式可以正确地使用数字,然后按预期方式将其打印出来。

You could use %c too. 您也可以使用%c The code that processes %c specifier reads the argument as an int , and then converts it to a char . 处理%c说明符的代码将参数读取为int ,然后将其转换为char 0x12 is a special character, though, so you would not see a uniform printout for it. 0x12是一个特殊字符,因此您不会看到统一的打印输出。

Since pointers are strongly typed, shouldn't a character pointer strictly point to a char data type? 由于指针是强类型的,因此字符指针不应该严格指向char数据类型吗?

This is kind of undefined behavior - but such that most sane implementations will do what you mean. 这是一种不确定的行为-但是大多数理智的实现都会按照您的意思执行。 So most people would say ok to it. 因此,大多数人都会同意。

And what's up with printing with %d? 用%d打印怎么办?

Format %d expects argument of type int, and the actual arg of type char is promoted to int by usual C rules. 格式%d期望使用int类型的参数,而普通char规则将char类型的实际arg提升为int。 So this is ok again. 所以这没关系。 You probably don't want to use %c since the content of byte pointed by p may be any byte, not always a valid text character. 您可能不希望使用%c,因为p指向的字节的内容可以是任何字节,并不总是有效的文本字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM