casting

O

omariqbalnaru

#include<iostream>
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?
 
F

Frederick Gotham

(e-mail address removed) posted:
#include<iostream>
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top