casting

B

Baloff

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;
printBinary(f);
}
}




******************************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.
 
K

Kelly Walker

Baloff said:
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;
printBinary(f);
}
}




******************************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); // Print each byte of float as binary
}
}
 

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

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top