Why isn't `pointer(Cint(0))` valid?


#1

I’m trying to wrap a library with ccall. The following yields an error:

a = Cint(0)
p = pointer(a)

But this works and I think does what I want:

a = Vector{Cint}(1); a[1]=0;
p = pointer(a)

But it feels awfully silly to have to do this. To pass a pointer to a Cint, is the latter the correct way to pass a Ptr{Cint} to a C function? Is there a good reason for not allowing pointer(Cint(0)) ?


#2

Use:

a = Ref{Cint}(0)
ccall((:func, :lib), Void, (Ptr{Cint},), a)

#3

Simon answered your first question; here’s a shot at the second

The statement a=Cint(0) just binds the symbol :a to an object of scalar bits type, which is immutable - i.e. managed by copying. There is no guarantee that it is ever even associated with a memory location, and it would encumber the compiler too much to allow user code to force such an association. (So it’s similar to a strictly enforced register storage class in C.)