简体   繁体   English

将int指针转换为char指针

[英]casting int pointer to char pointer

I've read several posts about casting int pointers to char pointers but i'm still confused on one thing. 我已经阅读了几篇关于将指针转换为char指针的帖子,但我仍然对一件事感到困惑。

I understand that integers take up four bytes of memory (on most 32 bit machines?) and characters take up on byte of memory. 我知道整数占用四个字节的内存(在大多数32位机器上?),字符占用内存字节。 By casting a integer pointer to a char pointer, will they both contain the same address? 通过将整数指针转换为char指针,它们是否都包含相同的地址? Does the cast operation change the value of what the char pointer points to? 强制转换操作是否会更改char指针指向的值? ie, it only points to the first 8 bits of an integers and not all 32 bits ? 即,它只指向整数的前8位而不是全部32位? I'm confused as to what actually changes when I cast an int pointer to char pointer. 当我将一个int指针转换为char指针时,我很困惑。

By casting a integer pointer to a char pointer, will they both contain the same address? 通过将整数指针转换为char指针,它们是否都包含相同的地址?

Both pointers would point to the same location in memory. 两个指针都指向内存中的相同位置。

Does the cast operation change the value of what the char pointer points to? 强制转换操作是否会更改char指针指向的值?

No, it changes the default interpretation of what the pointer points to. 不,它会更改指针指向的默认解释。

When you read from an int pointer in an expression *myIntPtr you get back the content of the location interpreted as a multi-byte value of type int . 当您从表达式*myIntPtrint指针读取时,您将获得解释为int类型的多字节值的位置的内容。 When you read from a char pointer in an expression *myCharPtr , you get back the content of the location interpreted as a single-byte value of type char . 当您从表达式*myCharPtrchar指针读取时,您将获取解释为char类型的单字节值的位置的内容。

Another consequence of casting a pointer is in pointer arithmetic. 转换指针的另一个结果是指针算术。 When you have two int pointers pointing into the same array, subtracting one from the other produces the difference in int s, for example 例如,当你有两个指向同一个数组的int指针时,从另一个中减去一个会产生int的差异

int a[20] = {0};
int *p = &a[3];
int *q = &a[13];
ptrdiff_t diff1 = q - p; // This is 10

If you cast p and q to char , you would get the distance in terms of char s, not in terms of int s: 如果你投pqchar ,你会得到来讲距离char S,不是来讲int S:

char *x = (char*)p;
char *y = (char*)q;
ptrdiff_t diff2 = y - x; // This is 10 times sizeof(int)

Demo. 演示。

Casting a pointer just changes how it is interpreted; 转换指针只会改变它的解释方式; no change to its value or the data it points to occurs. 不会更改其值或指向的数据。 Using it may change the data it points to, just as using the original may change the data it points to; 使用它可能会更改它指向的数据,就像使用原始数据可能会更改它指向的数据一样; how it changes that data may differ (which is likely the point of doing the casting in the first place). 如何改变数据可能会有所不同(这可能是首先进行投射的重点)。

The int pointer points to a list of integers in memory. int指针指向内存中的整数列表。 They may be 16, 32, or possibly 64 bits, and they may be big-endian or little endian. 它们可能是16位,32位或可能是64位,它们可能是big-endian或little endian。 By casting the pointer to a char pointer, you reinterpret those bits as characters. 通过将指针强制转换为char指针,可以将这些位重新解释为字符。 So, assuming 16 bit big-endian ints, if we point to an array of two integers, 0x4142 0x4300, the pointer is reinterpreted as pointing to the string "abc" (0x41 is 'a', and the last byte is nul). 因此,假设16位大端整数,如果我们指向两个整数的数组0x4142 0x4300,则指针被重新解释为指向字符串“abc”(0x41为'a',最后一个字节为nul)。 However if integers are little endian, the same data would be reinterpreted as the string "ba". 但是,如果整数是小端,则相同的数据将被重新解释为字符串“ba”。

Now for practical purposes you are unlikely to want to reinterpret integers as ascii strings. 现在出于实际目的,您不太可能想要将整数重新解释为ascii字符串。 However its often useful to reinterpret as unsigned chars, and thus just a stream of raw bytes. 然而,将其重新解释为无符号字符通常很有用,因此只是一个原始字节流。

A pointer is a particular variable that stores the memory address where another variable begins . 指针是一个特定的变量,用于存储另一个变量开始的内存地址。 Doesnt matter if the variable is a int or a char, if the first bit has the same position in the memory, then a pointer to that variable will look the same. 无论变量是int还是char,如果第一位在内存中的位置相同,那么指向该变量的指针看起来都是一样的。

the difference is when you operate on that pointer. 区别在于你操作指针时。 If your pointer variable is p and it's a int pointer, then p++ will increase the address that it contains of 4 bytes. 如果你的指针变量是p并且它是一个int指针,那么p++将增加它包含4个字节的地址。

if your pointer is p and it's a char pointer, then p++ will increase the address that it contains of 1 byte. 如果你的指针是p并且它是一个char指针,那么p++将增加它包含1个字节的地址。

this code example will help you understand: 这段代码示例将帮助您理解:

int main(){
    int* pi;
    int i;

    char* pc;
    char c;

    pi = &i;
    pc = &c;

    printf("%p\n", pi);    // 0x7fff5f72c984 
    pi++;
    printf("%p\n", pi);    // 0x7fff5f72c988

    printf("%p\n", pc);    // 0x7fff5f72c977
    pc++;
    printf("%p\n", pc);    // 0x7fff5f72c978
}

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

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