[swift-users] proper syntax for inout array handling?

Ken Burgett kenb at iotone.io
Thu Jun 9 15:38:38 CDT 2016

I am converting a very dirty C program to Swift3, and their is plenty of 
pointer arithmetic to be dealt with.  As part of the effort, I have 
created a 'memcpy clone to help with some of the transformation.
Here is the output from the REPL:
   1. var source = [UInt8](repeating: 0x1f, count: 32)
   2. var destination = [UInt8](repeating: 0, count: 64)
   4. func memcpy(dest: inout [UInt8], src: inout [UInt8], count: Int)
   5. {
   6.   for ix in 0...count-1
   7.   {
   8.     dest[ix] = src[ix]
   9.   }
  10. }
  12. memcpy(dest: &destination, src: &source, count: 32 )
  14. memcpy(dest: &destination[3], src: &source, count: 13)
error: repl.swift:14:26: error: cannot convert value of type 'UInt8' to 
expected argument type '[UInt8]'
memcpy(dest: &destination[3], src: &source, count: 13)
which shows me that the compiler does not like the form of 
&destination[13], and doesn't treat it as a [UInt8].

What is the correct syntax for using a base + offset as a source or 
destination for a memory copy?

Ken Burgett
Principal Software Engineer
Email: kenb at iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co

More information about the swift-users mailing list