cairo-lang
cairo-lang copied to clipboard
How to initialise an empty pointer (like nullptr in C++)?
In languages like C++, it is possible to create a pointer that points to nothing (nullptr). This is useful for initialising objects that point to other variables - but where the pointer is not yet defined at the point of initialisation.
Is it possible to do this in Cairo, or if not, what is the recommended workaround?
I'm not 100% sure, but I think the closest thing to that is let nullptr = cast(0, felt*)
. It's a pointer to 0
, same as an uninitialized memory cell. I guess it's more of an "undefined pointer" rather than a null pointer.
Also, often times when passing pointers around, you have an additional <POINTER_VAR_NAME>_size
or <POINTER_VAR_NAME>_len
param that is passed along with the pointer. In that case, the size/len param will be 0.
Thanks very much for your help with this! I initially closed the issue because it seems to work well for pointers to regular structs. However I would like to use this with a dictionary, and let nullptr = cast(0, DictAccess*)
returns the following error:
Error: [code:49:5](javascript:current_page().move_cursor(49 - 1, 5 - 1)): Cannot compare 'felt' and 'starkware.cairo.common.dict_access.DictAccess*'.
assert values[1] = cast(0, DictAccess*)
I thought maybe it is because I need to define nullptr as a pointer to DictAccess*
, but that didnt work either and resulted in a similar error..