What is & doing when using pointers?

From what I understand, & works with the inout keyword by copying the value both ways. However it also seems to work for functions that take one of the UnsafePointer types as an argument. Can I assume it’s passing an actual pointer in this case, or is that going to cause issues?

var x = 1

For C functions only, & passes a pointer to the value. I believe the compiler warns if you use & to pass an inout argument to a native Swift function that takes a pointer.

inout guarantees exclusive storage, which is needed because C functions that take pointer arguments often expect to be able to write to memory through the pointer, and those mutations would be lost if we passed a copy of x instead of x itself.

related reading:

1 Like

Huh, does that mean if I write my own Swift function that takes a pointer & doesn’t work? I am not getting any errors

I thought there was a warning about this, but I can’t get it to trigger.

Aha, I just found a relevant thread. It seems I was misremembering, or perhaps remembering a proposal buried in that thread or a conversation I’d had elsewhere.

1 Like

Oh wow, okay, thanks :smile: