MATLAB: MEX – ‘Initialization From Incompatible Pointer Type’ Warning

eMATLABMATLAB Compilermexmex compiler

Hello,
I'm learning basic MEX programming and have been writing some utility functions over the last couple of days. I noticed MATLAB doesn't have an inbuilt function to specify if elements of an array are even or odd, so I wrote one. This was the most straightforward one so far and the function works entirely as intended, but I'm left with a compiler warning that I can't shift. I'm not a fan of leaving warnings alone even if the code is functional:
C:\Work\MY_Utilities\iseven.c: In function 'mexFunction':
C:\Work\MY_Utilities\iseven.c:69:28: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
bool *output_ptr = mxGetPr(plhs[0]); // Get a pointer to start of output matrix
^~~~~~~
The offending code is here; I create a return matrix of logical values of the same dimensions as my input, specify a pointer to my output of the same type, and then use a loop to call my even/odd function (which returns a bool) and assign each element to the matrix:
// Create and fill logical array if input is 1-by-N row vector
if (n_cols > 1 && n_rows == 1)
{
plhs[0] = mxCreateLogicalMatrix(n_rows, n_cols); // Create output matrix
bool *output_ptr = mxGetPr(plhs[0]); // Get a pointer to start of output matrix
for (int i = 0; i < n_cols; i++)
{
output_ptr[i] = element_even(input_arr[i]);
}
mxFree(input_arr);
return;
}
I have used this approach in my other functions (i.e. creating an output matrix of doubles and then creating a pointer to a double, for example) without any warnings or issues. What have I gotten wrong and how can I clear this warning?
Thanks in advance!

Best Answer

Rowan - rather than using a bool perhaps you could try to use a mxLogical instead? Something like
plhs[0] = mxCreateLogicalMatrix(n_rows, n_cols); // Create output matrix
mxLogical *output_ptr = mxGetLogicals(plhs[0]);
for (int i = 0; i < n_cols; i++)
{
output_ptr[i] = (mxLogical)element_even(input_arr[i]);
}
I haven't tested the above and am not sure of the cast to mxLogical but i think that is what you might want to try to do (the cast would be unnecessary if the element_even function returns a mxLogical instead of a bool.
As for the warning, the documentation for mxLogical mentions how All logical mxArrays store their data elements as mxLogical rather than as bool. So the warning message does make sense (a cast to a bool may remove that warning but I think using mxLogical might be the preferred approach).