简体   繁体   English

错误:从'const prog_uchar *'转换为'byte'会失去精度吗?

[英]error: cast from 'const prog_uchar*' to 'byte' loses precision?

The error is at this line : 错误在这一行:

dataArray[iLedMatrix][iRow] |=  (byte)(bufferPattern[iRow]) & (1<<7);

dataArray is : byte dataArray[NUMBER_LED_MATRIX][NUMBER_ROW_PER_MATRIX]; dataArray为:字节dataArray [NUMBER_LED_MATRIX] [NUMBER_ROW_PER_MATRIX];

bufferPattern is : const patternp * bufferPattern; bufferPattern是:const patternp * bufferPattern;

patternp is a typedef of the type : typedef prog_uchar patternp[NUM_ROWS]; patternp是类型的typedef:typedef prog_uchar patternp [NUM_ROWS];

I can see in the Reference that prog_uchar is 1 byte ( 0 to 255 ). 我可以在参考文献中看到prog_uchar是1个字节(0到255)。 So I do not understand the error about losing precision? 所以我不明白关于失去精度的错误吗? Any idea? 任何想法?

The problem is in this sub expression 问题出在这个子表达式中

(byte)(bufferPattern[iRow])

The variable bufferPattern is of type const patternp * so when the indexer is applied the result is patternp. 变量bufferPattern的类型为const patternp *因此应用索引器时,结果为patternp。 The type "patternp" is typedef to prog_uchar[]. 类型“ patternp”是prog_uchar []的typedef。 So in actuality this expression is saying 所以实际上这个表达是说

Cast a prog_uchar* to a byte 将prog_uchar *强制转换为一个字节

Byte is almost certainly a single byte value and prog_uchar* is the platform specific pointer type (either 4 or 8 bytes). 字节几乎可以肯定是一个字节值,而prog_uchar *是平台特定的指针类型(4或8字节)。 This does indeed result in a loss of precision. 实际上确实导致精度损失。 Perhaps you meant to dereferenc this value? 也许您打算取消引用此值?

(byte)(*(bufferPattern[iRow]))

You are trying to cast from a pointer type to byte. 您试图将指针类型转换为字节。 A pointer type is usually represented on 4 bytes (32 bit OS) or 8 bytes (64 bits), and you are trying to convert its address value to 1 byte. 指针类型通常用4个字节(32位OS)或8个字节(64位)表示,并且您试图将其地址值转换为1个字节。

bufferPattern[ iRow ] resolves to a patternp , which is a prog_uchar[ NUM_ROWS ] . bufferPattern[ iRow ]解析为patternp ,它是prog_uchar[ NUM_ROWS ]

So you're actually casting an array (implemented as a pointer) to a byte. 因此,您实际上是将数组(实现为指针)转换为字节。 Makes no sense; 没有意义; lucky the compiler warned you! 幸运的是编译器警告您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从“void*”转换为“int”失去精度 - error: cast from 'void*' to 'int' loses precision 从“int*”转换为“unsigned int”会丢失精度错误 - cast from ‘int*’ to ‘unsigned int’ loses precision error 错误:从&#39;Foo *&#39;转换为&#39;unsigned int&#39;会失去精度 - error: cast from 'Foo*' to 'unsigned int' loses precision C ++编译错误:“从&#39;WCHAR *&#39;转换为&#39;WORD&#39;失去精度” - C++ compilation error: “cast from 'WCHAR*' to 'WORD' loses precision” 错误:从&#39;...&#39;转换为&#39;unsigned int&#39;会丢失精度[-fpermissive] - error: cast from ‘…’ to ‘unsigned int’ loses precision [-fpermissive] 错误:从&#39;char *&#39;转换为&#39;unsigned int&#39;会失去精度 - error: cast from 'char*' to 'unsigned int' loses precision C++ 错误:从“int*”转换为“int”失去精度 - C++ error: cast from ‘int*’ to ‘int’ loses precision C ++:从“常量变量*”转换为“ uint32”会失去精度 - c++: cast from “const variable*” to “uint32” loses precision 从&#39;BYTE * {aka unsigned char *}&#39;转换为&#39;DWORD {aka long unsigned int}&#39;会失去精度[-fpermissive] - cast from 'BYTE* {aka unsigned char*}' to 'DWORD {aka long unsigned int}' loses precision [-fpermissive] 从double *转换为int会失去精度 - cast from double* to int loses precision
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM