MEX - 'Initialization From Incompatible Pointer Type' Warning
Show older comments
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!
Accepted Answer
More Answers (0)
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!