Thread: Typecasting pointers

  1. #1
    Registered User
    Join Date
    Jan 2018

    Typecasting pointers


    I was studying typecasting with pointers when I got stuck on the following exercise:

    #include <stdio.h>
    int main() {
    int i; char char_array[5] = {'a','b','c','d','e'}; int int_array[5] = {1,2,3,4,5}; char_pointer = (char*) int_array; int_pointer = (int*) char_array; for(i = 0; i < 5; i++) {
    printf("[integer pointer] points to %p, which contains the char '%c'\n", int_pointer, *int_pointer); int_pointer = (int*)((char*) int_pointer+1;
    My question is the following and concerns about the instruction in bold:

    • Why do I have to typecast back the pointer? I know what happens if I don't typecast the pointer in order to see good results, but if I run the program without putting that (int*) before the ((char*) int_pointer+1) it works fine, with just a warning from the compiler.
      Actually I don't get what happens in memory in order to be necessary to typecast it back.

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You have to cast because there isn't an implicit version from char* to int*. It's a matter of type; nothing changes in memory, only how the memory is interpreted.

    It's rather longwinded to do this though: if I really wanted to be reinterpreting an array of char as ints like this, I would just use the loop index before the printf:
    int_pointer = (int*)&char_array[i];
    Btw, this exercise is a bit of a farce: if you want to interpret 5 bytes as ints, assuming 4 byte ints, you could do that in two ways: (int*)char_array[0] and(int*)char_array[1]. But (int*)char_array[2] onwards doesn't make sense since your int pointer would then be pointing partially to memory that doesn't belong to you.

    This means your printf results in undefined behaviour because you are dereferencing int_pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2018
    I see, so in memory nothing changes. Thank you, I just wanted to know this.

    Actually this exercise is just a part of different versions of the same task: this was just an example on how the same thing can be done using pointer that points do different types, but I couldn't get the reason of that typecasting back.

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typecasting
    By ~Kyo~ in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2011, 05:56 PM
  2. unclear about typecasting and creating pointers
    By kirani in forum C Programming
    Replies: 11
    Last Post: 06-16-2010, 04:29 PM
  3. C Typecasting Help
    By in forum C Programming
    Replies: 7
    Last Post: 03-11-2010, 02:26 AM
  4. Typecasting and void pointers...
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 08-04-2008, 10:07 PM
  5. typecasting void *pointers
    By Eavan Hyde in forum C Programming
    Replies: 16
    Last Post: 07-03-2006, 08:33 AM

Tags for this Thread