Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store a signed integer in an integer pointer (int *)?

What's special about integer pointers (but any pointer type really) is that you can assign to them NULL a non-integer sort of value; whereas an integer has to, no matter what, store an integer, be it a positive or negative one (correct me if I'm wrong), and no NON-integer values.

Could you then make use of this 'special' feature of pointers (that of being a variable that can store integers and a NON-integer value: NULL) to at the same time store integer values (via their literal actual value, like instead of an address, it would store a signed/unsigned integer) and be a sort of boolean -- where a NULL pointer would signify false & a valid pointer (i.e one that is holding an integer) (but not really valid ofc, just one that isn't NULL) would signify true.

Note: The pointer is absolutely never used to access a memory location, just to store an int.

(Ofc this is just for a particular use case, not that you would do this in regular code)


(If you really want to know) I'm trying to make a recursive function, and want the return value of the function to return an integer but also want to keep track of a condition, so I also want it to return a boolean, but you obviously can only return a single argument... so could passing an integer pointer, a variable that can do both at once, be the solution ?

I thought of other ways (stucts, arrays.. ), but curious if doing it w/ an integer pointer could be a plausible way.

like image 253
AymenTM Avatar asked Feb 14 '26 03:02

AymenTM


2 Answers

There’s nothing special about a pointer with regard to NULL. On modern Intel based implementations not running in 8086 real mode, a pointer is just an unsigned integer, and NULL is 0. You can’t store something “extra” in that way.

If you need to return two values from your function, create a struct containing an int and a bool and have your function return that.

like image 69
dbush Avatar answered Feb 15 '26 15:02

dbush


Is it possible to store a signed integer in an integer pointer (int *)?

Maybe. It might "work". Even the assignment, without de-referencing the pointer, may cause the program to stop. Even with a successful conversion, information may be lost.

An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation C11dr §6.3.2.3 5

// Sample implementation
int i = -rand();
printf("i: %d\n", i);
int *iptr = (int *) i;   // implementation-defined
printf("iptr: %p\n", (void*) iptr);

What's special about integer pointers (?)

They are correctly valued to aligned on de-referencing to point to the specific integer type. They may exist in an address space that is not suitable for some other types. C even allows for a int * to be narrower than a void *. (Have not seen a machine take advantage of that in some time though.)


.. an integer has to, no matter what, store an integer ...

Counter examples: Code can store a _Bool in a integer and be recovered unchanged. void * can be save in a integer of type (u)intptr_t and be recovered with an equivalent value.


A integer of the optional type (u)intptr_t can convert to a void* and maintain pointer equivalence. This is not necessarily true with direct casting of other non-character pointers or of function pointers. This is not not necessarily true with other integer types.

some_type_t *some_valid_object_pointer = ...;
intptr_t i = (intptr_t)(void*) some_valid_object_pointer;
some_type_t *some_valid_object_pointer2 = (some_type_t*)(void*)i;
assert(some_valid_object_pointer == some_valid_object_pointer2);

Could you then make use of this 'special' feature of pointers

Not certainly. OP's implementation may work on selective platform, but is it lacks specified behavior and portability.

like image 44
chux - Reinstate Monica Avatar answered Feb 15 '26 15:02

chux - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!