Thread: data type mis-match in pointers

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    data type mis-match in pointers

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    void main()
    {
    int a=10,*x=&a;
    float *b;
    float c;
        clrscr();
        b=&a;
        x=&a;
       printf("%d \n%u \n%u \n%d \n%f",a,b,x,*x,*b);
       getch();
    }
    Output :
    10
    65524
    65524
    10
    0.000000

    When a float pointer is able to store the address of an integer why cannot it access it?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Storing the address does not make the data compatible.

    C does not check for these things at compile time or at run time... it is up to you as a programmer to know and stay within the bounds of good practice.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    For pointers, what i've tried or read so far is, datatype of the pointer should match the variable, to be pointed by the pointer....

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You can access the pointer, but you can not interpret what it points to interchangeably as either int or float. The way the data is represented is entirely different.

    Compilers should give warnings when you try to assign one pointer with the value of another that's a different type. Just so it keeps you out of trouble.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.777 View Post
    For pointers, what i've tried or read so far is, datatype of the pointer should match the variable, to be pointed by the pointer....
    That's correct. BUT as the OP discovered, nothing stops us from reassigning that pointer to almost anything at run time.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yes, we can keep pointers pointing to anything at anytime unless pointer is declared as constant pointer......

    So, the reason i think is;
    It tries to type cast the values and in pointer's case, it fails... I can't check this at the moment as i got no compiler and i can't even install it due to some official problems :-)
    By the way, why do you need to point it to integer?
    Or try making integer pointer to point float and see what results do you get......

    May be some of machine dependencies????
    You are using c++ and integer is of 4 bytes and float is of 4 bytes as well.....
    So, why it really doesn't show that????
    Really confusing.....

    Anyways, i try to search and all others also... May be someone knows the exact reason....
    Last edited by Mr.777; 03-07-2011 at 08:00 AM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Just because ints and floats may have the same size storage, it still doesn't mean that the number 10 as an integer is still a "10" when as a float the bits could be in the exponent section, or in the significand section, or whether the endianness of the machine would even put them in the expected area.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  3. I'm dreaming of making insertion sort O(nlogn)
    By RichSelian in forum C++ Programming
    Replies: 28
    Last Post: 03-28-2010, 12:18 PM
  4. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM