How to get a float type pointer to the data

    uchar* cv::Mat::data returns a pointer to the data of Mat. But the pointer type is uchar. Taking the following code as an example, what I need is float type pointer because the dtype is CV_32FC1. How can I get the float pointer to the data of CV_32F Mat.

int main()
{
	float src[75];
	for (int i = 0; i < 75; i++)
	{
		src[i] = (float)i;
	}

	auto B = cv::Mat(Size(3, 25), CV_32FC1, src);
	const uchar* dataindex = B.datastart;
	int index = 0;
	while (dataindex != B.dataend)
	{
		printf("Index:%d, Data:%d\n", index, * dataindex);
		dataindex++;
	}

Just learn casting types, it is a basic thing in C/C++.

float *dataindex=(float*)B.datastart;

And don’t use const if you intend to modify it ("dataindex++")! And printf uses %f to display float numbers.

For this case I recommend to use Mat iterators, it is a cleaner approach:

MatIterator_<float> mat_it = B.begin<float>();
while(mat_it!=B.end<float>()){
    printf("Data %f\n", *mat_it);
    mat_it++;
}

there is also:

float *ptr = B.ptr<float>(row_index);