Unable to return NULL in a function that expects an integer return type

Unable to return NULL in a function that expects an integer return type

Questions : Unable to return NULL in a function that expects an integer return type

854

Warning: return makes integer from pointer in4codes_c without a cast [enabled by default] return in4codes_c NULL;

I am trying to return NULL in my fucntion, in4codes_c but it’s not working out and I don’t in4codes_c understand why it is so. Below is my code, in4codes_c but I will not post the entire thing since I in4codes_c am only having problems with the return in4codes_c statement. I hope you can understand.

#include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct { int* data; unsigned int len; } intarr_t; int intarr_save_binary( intarr_t* ia, const char* filename ) { if(ia == NULL) { return NULL; // problem here, I want to return NULL if intarr_t*ia is null } else { // remaining code here, but is uncessary return 0; } } 

My final question is also how do I check if in4codes_c the char array filename is NULL?

Would I do this:

if (filename == NULL) { return NULL; } 

Total Answers 3
27

Answers 1 : of Unable to return NULL in a function that expects an integer return type

If the return type is int, you can’t in4codes_c return a NULL. To show an error, you in4codes_c could instead return a special value in4codes_c like zero or -1, if you check for that in4codes_c value in any calling function. Lots of in4codes_c functions return nonnegative numbers on in4codes_c success, or -1 on error.

NULL cannot be stored in an int in4codes_c variable, unlike in SQL, for example. If in4codes_c you ignore the warning and return NULL in4codes_c anyway, then NULL will be casted to in4codes_c zero. The calling function won’t be able in4codes_c to tell whether you returned NULL or in4codes_c zero.

If your function only needs to indicate in4codes_c success or failure, then it’s common to in4codes_c return 1 for success, and zero for in4codes_c failure. Zero means “false” when treated in4codes_c as a boolean value (like in if in4codes_c statements), and non-zero means “true.”

0

4

Answers 2 : of Unable to return NULL in a function that expects an integer return type

It looks like you’ve misunderstood what in4codes_c NULL means in C. Types are not nullable. in4codes_c NULL is effectively just a shorthand for in4codes_c the pointer with value 0! And int is not in4codes_c a pointer.

0

4

Answers 3 : of Unable to return NULL in a function that expects an integer return type

try

int? intarr_save_binary( intarr_t* ia, const char* filename ) 

0