MEX - 'Initialization From Incompatible Pointer Type' Warning
9 views (last 30 days)
Show older comments
Rowan Lawrence
on 28 Feb 2020
Commented: Rowan Lawrence
on 28 Feb 2020
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!
0 Comments
Accepted Answer
Geoff Hayes
on 28 Feb 2020
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).
2 Comments
James Tursa
on 28 Feb 2020
Edited: James Tursa
on 28 Feb 2020
The warning message is because mxGetPr( ) returns a pointer_to_double type which is incompatible with pointer_to_bool, not because of any fundamental difference between bool and mxLogical.
That being said, I agree with your advice that mxLogical and mxGetLogicals( ) are the proper choices here.
More Answers (0)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!