[英]C++ Type Casting: error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK'
I have this types: 我有以下类型:
typedef short DCTELEM;
typedef DCTELEM DCTBLOCK[64];
Array of last type and a pointer to a malloc'ed array of shorts: 最后一个类型的数组和一个指向malloc分配的短裤数组的指针:
DCTBLOCK MQUAD;
short * ptrArray;
I need MQUAD to bet to specific location pointed to by ptrArray; 我需要MQUAD下注到ptrArray指向的特定位置;
In CI would prolly write something like 在CI中会写类似
MQUAD = ptrArray + 3 * 2;
and have MQUAD after that pointing to a needed location, but I get 然后将MQUAD指向所需的位置,但是我得到了
error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK' 错误C2440:“ =”:无法从“短*”转换为“ DCTBLOCK”
in c++, cause I know there is a difference in a type of array and a pointer to some-type. 在C ++中,因为我知道数组类型和指向某种类型的指针之间存在差异。
Your MQUAD
variable is an array, not a pointer, so you can't assign to it (thanks to Remy Lebeau's comment). 您的MQUAD
变量是一个数组,而不是一个指针,因此无法分配给它(感谢Remy Lebeau的评论)。 If you declare it as: 如果您声明为:
DCTELEM *MQUAD;
then you can assign to it: 然后您可以为其分配:
MQUAD = reinterpret_cast<DCTELEM *>(ptrArray + 3 * 2);
This is using the C++ cast syntax. 这是使用C ++强制转换语法。 You can use C cast syntax if you like too. 如果愿意,也可以使用C强制转换语法。
The main mistake is 主要错误是
DCTBLOCK MQUAD // it's wrong
DCTELEM* MQUAD // it's right
and you may cast as u want 你可以随心所欲
MQUAD = ptrArray + 3 * 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.