简体   繁体   English

在 C 中将 int 列表转换为 char 列表时会发生什么?

[英]What happens when a int list is cast as a char list in C?

In the example below, I have attempted to convert a list of ASCII integer values (representing "a, b, c") from an integer list to a character list:在下面的示例中,我尝试将 ASCII 整数值列表(表示“a、b、c”)从整数列表转换为字符列表:

int main() {

   int myList[3] = {97, 98, 99}; // a, b, c in ASCII
   char * myCharList = (char*) myList;
   
   printf("%c\n", myCharList[0]);
   printf("%c\n", myCharList[1]);
   printf("%ld\n", strlen(myCharList));
   return 0;
}

Output:输出:

a

1

Why does casting the int list create a character list that is only of length 1?为什么转换 int 列表会创建一个长度仅为 1 的字符列表?

Edit:编辑:

I think I am starting to understand now.我想我现在开始明白了。 So as another example, if I changed the code like this:再举一个例子,如果我像这样更改代码:

#include <stdio.h>
#include <string.h>

int main() {

   int myList[3] = {255 + 101, 98, 99};
   char * myCharList = (char*) myList;

   printf("%c\n", myCharList[0]);
   printf("%c\n", myCharList[1]);
   printf("%ld\n", strlen(myCharList));
   return 0;
}

Then the bytes would be 100, 255, 0, 0. Then the output would be:那么字节将是 100, 255, 0, 0. 然后输出将是:

d

2

And because an integer is two bytes, this pointer can not be a list longer than length 2.并且因为一个整数是两个字节,这个指针不能是一个长度超过 2 的列表。

You did not convert an integer list to a character list.您没有将整数列表转换为字符列表。

myList is an array of int . myList是一个int数组。 However, when an array is used in most expressions 1 , it is converted to a pointer to its first element.但是,当在大多数表达式1 中使用数组时,它会被转换为指向其第一个元素的指针。

So (char*) myList converts a pointer to myList[0] to a pointer to char .所以(char*) myList将指向myList[0]的指针转换为指向char的指针。 No conversion of the list is performed;不执行列表转换; only the pointer is converted.只有指针被转换。

The result is that myCharList points to the bytes of myList in memory.结果是myCharList指向内存中myList的字节。

In your system, int values are stored with the least significant byte first.在您的系统中, int值首先以最低有效字节存储。 Likely, they have four bytes, so the bytes representing the int 97 are 0, 0, 0, and 97, in order from most significant to least significant.很可能,它们有四个字节,因此表示int 97 的字节是 0、0、0 和 97,按从最重要到最不重要的顺序。 So the bytes in memory, from the low address to the high address, are 97, 0, 0, 0.所以内存中的字节,从低地址到高地址,是97、0、0、0。

When you apply strlen to this, it sees the 97 byte and then it sees a zero byte, which marks the end of the string.当您将strlen应用于此时,它会看到 97 字节,然后会看到一个零字节,它标志着字符串的结尾。 So strlen reports the length of this “string” in memory is 1.所以strlen报告这个“字符串”在内存中的长度是 1。

To convert a list of integers to a list of character strings containing numerals for those integers, such as converting 97 to “97”, you must use routines like sprintf (which can format numbers like printf but write the results into a character array instead of to standard output) or write other source code to perform the conversion.要将整数列表转换为包含这些整数的数字的字符串列表,例如将 97 转换为“97”,您必须使用像sprintf这样的例程(它可以像printf一样格式化数字,但将结果写入字符数组而不是到标准输出)或编写其他源代码来执行转换。 And you must provide memory for where the characters will be written, by defining arrays large enough to hold the results or using malloc to allocate space.并且您必须通过定义足够大的数组来保存结果或使用malloc分配空间来为字符将被写入的位置提供内存。

Footnote脚注

1 When it is not the operand of sizeof or unary & and is not a string literal used to initialize an array. 1当它不是sizeof或一元&的操作数并且不是用于初始化数组的字符串文字时。

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

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