<div dir="ltr"><div><div>Personally I like <a target="_blank" href="http://swiftdoc.org">swiftdoc.org</a> as a reference:<br><br><a target="_blank" href="http://swiftdoc.org/v3.0/type/UnsafeMutablePointer/">http://swiftdoc.org/v3.0/type/UnsafeMutablePointer/</a><br><br></div>Click on the initializers and function names to read the description of what each of them does.<br><br></div>You can use a UnsafeMutableBufferPointer to help initialize your memory (which you have to allocate first):<br><br><span style="font-family:monospace,monospace">var memory = UnsafeMutablePointer<Int8>(allocatingCapacity: 4)<br>var buf = UnsafeMutableBufferPointer<Int8>(start: memory, count: 4)<br>for i in 0...3 {<br> buf[i] = Int8(0x10 * (i + 1)) // [ 16, 32, 48, 64 ]<br>}<br>CFunctionThatTakesACharPointer(memory, 4)</span><br><br><div><div>--<br>Bryan Chan</div></div><div class="gmail-yj6qo gmail-ajU"><div tabindex="0" class="gmail-ajR" id="gmail-:ns"><img src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif" class="gmail-ajT"></div></div><span class="gmail-im">
<br><div class="gmail_quote">On Fri, Jul 29, 2016 at 11:40 PM, Chris McIntyre via swift-users <span dir="ltr"><<a target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a>></span> wrote:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="word-wrap:break-word">Another
problem. I have a specific byte pattern I want to create. For arguments
sake, lets call it 0x123ABC, and I have it as an Int. I want to access
the individual bytes (i.e. 12, 3A, BC). <div><br></div><div>The struct
reference for UnsafePointer<T> doesn’t talk much about
initializing it. Most of the initializers take a pointer. I tried the
init(_ bitPattern:) initializer, and was able to create a pointer, but
it seemed to point to the address 0x123ABC rather than the address *of*
0x123ABC. I tried creating a buffer with malloc, and it gives me an
UnsafeMutablePointer but now I can’t figure out how to copy my bytes to
this buffer.</div></div></blockquote></div></span></div>