Thread: Pointer type casting

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Pointer type casting

    I have a program like below

    Code:
    #include<stdio.h>
    
    int main()
    {
      int intVal = 10;
      short shVal;
      char chVal;
    
      char *pChar;
    
    
      pChar = (char *)&intVal;
    //  pShort = &shVal;
    //  pChar = &chVal;
    
    //    intVal = 10;
    //  *pShort = 20;
    //  *pChar = 'q';
    
      printf("intVal %c \n",(int *)pChar);
    
      return 0;
    }
    Here my intention is to type cast the integer variable to a pointer and using that

    pointer variable i want to display the value .

    Can i know where am i missing..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are currently printing a char (%c) but passing a char pointer cast into int * - thus it does not print the value stored at that location, but the location where it is stored [as a char].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    *((int *)val);
    But why do you want to do something absurd like this ?
    Last edited by PING; 08-11-2008 at 07:33 AM. Reason: typo
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM