Skip to content


Dealing with “Warning: return makes integer from pointer without a cast” (C/C++)

This error means that an implicit type casting takes place when returning the result at the end of the function. In this case, the function was defined as returning an int(eger) while the returned variable wasn’t one. The fix was obviously to correct the definition of the function (or, if appropriate, to cast the returned variable into an int before returning it).
Personally, I encountered that error while trying to make a simple hello world R function, as I’m not really good at playing with R’s custom types: I defined my function to return an int, while it was in fact returning a “SEXP” (whatever that is… :D).

Bonus: here’s the fixed code (I’ll try to publish the whole R Hello World package when I’m done with it):

#include <R.h>
#include <Rinternals.h>

SEXP helloWorldC(SEXP n, SEXP m)
{
double *nn = REAL(n), *mm = REAL(m);
double *res = NULL;
SEXP result;

/* allocate and initialize result to zero. */
PROTECT(result = allocVector(REALSXP, 1));
res = REAL(result);
*res = 0;

*res = *nn * *mm;
if(*res==42) *res=1;
else *res=0;

UNPROTECT(1);
return result;
}

Bonus 2: if you want to read more about type casting in C++ (yes, I know the code above is in C ;)).

Posted in programming, R (R-project).


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA