<div dir="ltr"><p>Swift handling of Arrays isn’t like C (for which you can use <code>UnsafePointer</code>)-you don’t access the pointer of the third element. Instead, you can use <code>Array</code> slices, which makes it a one liner:</p>
<pre><code>destination[destinationStartIndex..<destinationStartIndex + count] = source[sourceStartIndex..<sourceStartIndex + count]
</code></pre><p>In your case,</p>
<pre><code>destination[0..<0 + 32] = source[0..<0 + 32]
destination[3..3 + 13] = source[0..<0 + 13]
</code></pre><p>As a function:</p>
<pre><code>func memcpy(source: [UInt8], destination: inout [UInt8], sourceStartIndex: Int = 0, destinationStartIndex: Int = 0, count: Int) {
destination[destinationStartIndex..<destinationStartIndex + count] = source[sourceStartIndex..<sourceStartIndex + count]
}
memcpy(source: source, destination: &destination, count: 32)
memcpy(source: source, destination: &destination, destinationStartIndex: 3, count: 13)
</code></pre><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 9, 2016 at 1:38 PM Ken Burgett via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I am converting a very dirty C program to Swift3, and their is plenty of<br>
pointer arithmetic to be dealt with. As part of the effort, I have<br>
created a 'memcpy clone to help with some of the transformation.<br>
Here is the output from the REPL:<br>
================================<br>
1. var source = [UInt8](repeating: 0x1f, count: 32)<br>
2. var destination = [UInt8](repeating: 0, count: 64)<br>
3.<br>
4. func memcpy(dest: inout [UInt8], src: inout [UInt8], count: Int)<br>
5. {<br>
6. for ix in 0...count-1<br>
7. {<br>
8. dest[ix] = src[ix]<br>
9. }<br>
10. }<br>
11.<br>
12. memcpy(dest: &destination, src: &source, count: 32 )<br>
13.<br>
14. memcpy(dest: &destination[3], src: &source, count: 13)<br>
error: repl.swift:14:26: error: cannot convert value of type 'UInt8' to<br>
expected argument type '[UInt8]'<br>
memcpy(dest: &destination[3], src: &source, count: 13)<br>
==================================<br>
which shows me that the compiler does not like the form of<br>
&destination[13], and doesn't treat it as a [UInt8].<br>
<br>
What is the correct syntax for using a base + offset as a source or<br>
destination for a memory copy?<br>
<br>
<br>
--<br>
Ken Burgett<br>
Principal Software Engineer<br>
Email: <a href="mailto:kenb@iotone.io" target="_blank">kenb@iotone.io</a><br>
Office: 530.693.4449<br>
Mobile: 831.332.6846<br>
URL: <a href="http://www.iotone.co" rel="noreferrer" target="_blank">www.iotone.co</a><br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr">-Saagar Jha</div></div>