casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • omariqbalnaru@inbox.com

    #1

    casting

    #include<iostre am>
    using namespace std;

    int main()
    {

    int i, *pi;
    float f, *pf;

    i = 1024;
    pi = &i;

    pf = (float *) pi;

    f = *pf;

    cout << " i = " << i << " f = " << f << "\n \n";


    f = i;

    cout << " i = " << i << " f = " << f << "\n \n";

    return 0;

    }

    In the statement:

    pf = (float *) pi;

    of the above program. Is that what we call casting of a int pointer to
    a float pointer?

  • Frederick Gotham

    #2
    Re: casting

    omariqbalnaru@i nbox.com posted:
    #include<iostre am>
    using namespace std;
    >
    int main()
    {
    >
    int i, *pi;
    float f, *pf;
    >
    i = 1024;
    pi = &i;
    >
    pf = (float *) pi;

    Here you cast an "int*" to "float*".

    The resultant address may very well be corrupted. The following isn't
    guaranteed to work:

    int i;

    int *pi = &i;

    float *pf = (float*)&i;

    int *pi2 = (int*)pf;

    assert(pi2 == pi);

    --

    Frederick Gotham

    Comment

    Working...