Thread: pointer question

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    pointer question

    OK.. This is a classic beginner question..

    We've got:

    double x = 100.1; double y; int *p;

    and I do this:

    p = (int *)&x; y = *p;

    Obviously the value of x is not assigned to y.. How can I avoid such mistakes in a program?

    Thank you.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    It really depends, what error/mistake are you talking about?

    1. If you're going to point to a double, use a pointer to a double (double* myDoublePtr).

    2. If you're going to screw it up anyway, I think you can get that code to work like this:

    Code:
    double x = 100.1; double y; int *p;
    
    p = (int *)&x;
    
    y = *((double*)p);
    However, I haven't tested that code, I just believe that rational thinking makes it right. Any pointer is just a pointer, so it occupies the same amount of memory, it has the size to hold a memory address. When you make an int pointer point to a double, you're making it point at the memory location where that double is, so in reality, it's the same as having any other pointer pointing to that area.

    The "real" problem begins when you assign the value that resides at the memory address pointed to by your pointer. Since you are treating the contents of that memory address as the contents of an int, the compiler will copy the value of the integer and assign it to the double. But that's not an integer! So whatever bits are there, they DO NOT represent an integer, so you'll be assigning an integer to a double that isn't even an integer. By assigning I mean:
    Code:
    int a = 50;
    double b = a;
    The above code implicitly assigns the integer a to b, which is what you do when you dereference your p pointer. (remember, the type of *p is int!). Hence, if you cast the pointer to double* (pointer to double), the compiler will interpret the data at that memory address as a double and, instead of assigning the integer that lives there, it assigns the double. Someone correct me if I threw a lot of BS in here.

    So, anyway, you shouldn't be using "int*" when you're going to point to a double. Use "double*"
    Last edited by Jorl17; 09-22-2010 at 10:53 AM.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Cpro0, can you please tell me/us what exactly you want to achieve? Because I truly have no idea whatsoever, thus, can't help you.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    I am just trying to understand what not to do with pointers because they are very tricky..

    Jorl17 has answered my question!

    Thank you!

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Heh, don't do what you have done. Casting a double to an int and assigning it to an int pointer.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Jorl17 pretty much nailed the point. Data stored in memory have no type associated with them, so the compiler uses the type of the variable as an indication of how to interpret the data. The bit representation of a double is much different than that of an int (plus it's probably larger too!). Thus, what you actually assign to your double is some garbage value that makes no sense.

    Use proper types and you won't have problems!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM