By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,129 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,129 IT Pros & Developers. It's quick & easy.

casting

P: n/a

Hello

I am struggling with this exercise

************************************************** ********************
Define a float variable. Take its address, cast that address to an
unsigned char, and assign it to an unsigned char pointer. Using this
pointer and [ ], index into the float variable and use the
printBinary( ) function defined in this chapter to print out a map
of the float (go from 0 to sizeof(float)). Change the value of the
float and see if you can figure out what\u2019s going on (the float
contains encoded data).

the printBinary.cpp is
#include "printBinary.h"
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val & (1 << i))
cout << "1";
else
cout << "0";
}

************************************************** ********************

my initial code which puts out the error below is as follows

******************************code**************** **************
#include <iostream>
#include "../printBinary.h"

using namespace std;

int main(){
float f = 3.3;
unsigned char* ucp = (unsigned char) &f;

for (int i = 0; i < sizeof(f); ++i){
ucp[i] = i;
printBinary(f[i]);
}
}


******************************error*************** ***************
main.cpp: In function `int main()':
main.cpp:8: warning: cast from pointer to integer of different size
main.cpp:8: error: invalid conversion from `unsigned char' to `unsigned char*'
main.cpp:12: error: invalid types `float[int]' for array subscript
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.

Aug 4 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Baloff" <wa****@wash.edu> wrote in message news:87************@wash.edu...

Hello

I am struggling with this exercise

************************************************** ********************
Define a float variable. Take its address, cast that address to an
unsigned char, and assign it to an unsigned char pointer. Using this
pointer and [ ], index into the float variable and use the
printBinary( ) function defined in this chapter to print out a map
of the float (go from 0 to sizeof(float)). Change the value of the
float and see if you can figure out what\u2019s going on (the float
contains encoded data).

the printBinary.cpp is
#include "printBinary.h"
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val & (1 << i))
cout << "1";
else
cout << "0";
}

************************************************** ********************

my initial code which puts out the error below is as follows

******************************code**************** **************
#include <iostream>
#include "../printBinary.h"

using namespace std;

int main(){
float f = 3.3;
unsigned char* ucp = (unsigned char) &f;

for (int i = 0; i < sizeof(f); ++i){
ucp[i] = i;
printBinary(f[i]);
}
}


******************************error*************** ***************
main.cpp: In function `int main()':
main.cpp:8: warning: cast from pointer to integer of different size
main.cpp:8: error: invalid conversion from `unsigned char' to `unsigned
char*'
main.cpp:12: error: invalid types `float[int]' for array subscript
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.


Make it:

#include <iostream>
#include "../printBinary.h"

using namespace std;

int main(){
float f = 3.3;
unsigned char* ucp = (unsigned char*) &f;
for (int i = 0; i < sizeof(f); ++i){
printBinary(ucp[i]); // Print each byte of float as binary
}
}

Aug 4 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.