9
votes

I've read several posts about casting int pointers to char pointers but i'm still confused on one thing.

I understand that integers take up four bytes of memory (on most 32 bit machines?) and characters take up on byte of memory. By casting a integer pointer to a char pointer, will they both contain the same address? Does the cast operation change the value of what the char pointer points to? ie, it only points to the first 8 bits of an integers and not all 32 bits ? I'm confused as to what actually changes when I cast an int pointer to char pointer.

4
integers take up four bytes ... it depends on platformLPs
@LPs The C specification guarantees that sizeof(char) == 1.Carey Gregory
@CareyGregory Well, my bad. I meant that on some architecture (DSP) one byte of memory is 32 bits.LPs
@LPs Yeah, that could be. The spec doesn't say how many bits a byte is.Carey Gregory

4 Answers

11
votes

By casting a integer pointer to a char pointer, will they both contain the same address?

Both pointers would point to the same location in memory.

Does the cast operation change the value of what the char pointer points to?

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. 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.

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 ints, for example

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 chars, not in terms of ints:

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

Demo.

2
votes

The int pointer points to a list of integers in memory. They may be 16, 32, or possibly 64 bits, and they may be big-endian or little endian. By casting the pointer to a char pointer, you reinterpret those bits as characters. 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). However if integers are little endian, the same data would be reinterpreted as the string "ba".

Now for practical purposes you are unlikely to want to reinterpret integers as ascii strings. However its often useful to reinterpret as unsigned chars, and thus just a stream of raw bytes.

1
votes

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).

1
votes

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.

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.

if your pointer is p and it's a char pointer, then p++ will increase the address that it contains of 1 byte.

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
}