Thread: Pointers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Type casting Pointers

    Hi everyone,

    I learn type casting of pointers.

    Could someone explain me why the last printf prints 0.00? I expect to print 10. What I am doing wrong?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    
        int a, *Pa;
        float b, *Pb;
        double c, *Pc;
        char d, *Pd;
    
    
        Pa = &a; // Pa points the address of a
        Pb = &b; // Pa points the address of b
        Pc = &c; // Pa points the address of b
        a = 10;
        b = 2.25;
    
    
        printf("The address of a is %d\n", Pa);
        printf("The value of a is %d\n", *Pa);
        printf("The address of Pa is %d\n", &Pa);
    
    
        // What is we want to assign the value of Pa to Pb? Pa is a variable of type int while Pb is a variable of type float
        // We have to use type-casting
    
    
        Pb = (float *)Pa;
        
        printf("The value of b is %.2f\n", *Pb); // Dereferencing Pb to read the value stored in the address the Pb pointer points
    
    
        return 0;
    }
    Pointers-screenshot_181-png
    Last edited by Nikosant03; 11-14-2021 at 11:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  4. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  5. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM

Tags for this Thread