铸件 [英] casting

查看:60
本文介绍了铸件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好


我正在努力练习这个练习


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

定义一个浮点变量。获取其地址,将该地址转换为

unsigned char,并将其分配给unsigned char指针。使用这个

指针和[],索引到float变量并使用本章定义的

printBinary()函数打印出一张地图
$ b float的$ b(从0到sizeof(float))。更改

浮点数的值,看看你是否可以找出正在进行的内容(浮动

包含编码数据)。

printBinary.cpp是

#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;

}


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


我的初始代码输出如下错误如下


****************** ************代码**************** **************

#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]);

}

}



********** ********************错误*************** ************** *

main.cpp:在函数`int main()''中:

main.cpp:8:警告:从指针转换为不同大小的整数

main.cpp:8:错误:从`unsigned char''无效转换为`unsigned char *''

main.cpp :12:错误:无效类型`float [int]''对于数组下标

make:*** [main.o]错误1

make:目标`proj1 ''因为错误而没有重新制作。


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.

推荐答案

" Baloff" < WA **** @ wash.edu>在留言新闻中写道:87 ************ @ wash.edu ...
"Baloff" <wa****@wash.edu> wrote in message news:87************@wash.edu...

你好

我正在努力这个练习

***************************************** ********** ********************
定义一个浮点变量。获取其地址,将该地址转换为
unsigned char,并将其分配给unsigned char指针。使用这个
指针和[],索引到float变量并使用本章定义的
printBinary()函数打印出float的地图(从0到sizeof(浮动))。更改
浮动的值,看看你是否可以弄清楚正在进行的内容(浮动
包含编码数据)。

printBinary.cpp是
#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;
其他
cout<< 0;
}

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

我的初始代码推出以下错误如下

******************************代码*** ************* **************
#include< iostream>
#include" ../ printBinary。 h

使用命名空间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 [我]);
}
}


************************ ******错误*************** ***************
main.cpp:在函数`int main中()'':
main.cpp:8:警告:从指针强制转换为不同大小的整数
main.cpp:8:错误:从`unsigned char''无效转换为`unsigned
char *''
main.cpp:12:错误:无效类型`float [int]''对于数组下标
make:*** [ma in.o]错误1
make:目标`proj1''因错误而未重新制作。

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.




制作:


#include< iostream>

#include" ../ printBinary.h"


using namespace std;


int main(){

float f = 3.3;

unsigned char * ucp =(unsigned char *)& f;

for(int i = 0;我<的sizeof(F); ++ i){

printBinary(ucp [i]); //将float的每个字节打印为二进制

}

}



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
}
}


这篇关于铸件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆