Pointer type casting

This is a discussion on Pointer type casting within the C Programming forums, part of the General Programming Boards category; I have a program like below Code: #include<stdio.h> int main() { int intVal = 10; short shVal; char chVal; char ...

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

    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
    India
    Posts
    515
    Code:
    *((int *)val);
    But why do you want to do something absurd like this ?
    Last edited by PING; 08-11-2008 at 08:33 AM. Reason: typo
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08: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, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21