18312

How to decleare char *const argv[] in swift [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/29469158/how-to-pass-an-array-of-swift-strings-to-a-c-function-taking-a-char-parameter" dir="ltr" rel="nofollow">How to pass an array of Swift strings to a C function taking a char ** parameter</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

I am trying to make a c style array of chars pointer like this:

*const argv[]

I figured out i have to use UnsafePointer<UnsafeMutablePointer<Int8>>

but i don't know how to initialize it.

How can i map this normal Array to UnsafePointer<UnsafeMutablePointer<Int8>> :

let argv = ["/usr/bin/printf", "BBB"]

Thank you

Answer1:

The easy way is to let Cocoa form the C strings for you:

let args = ["/usr/bin/printf","BBB"] var cs = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.alloc(2) for (ix,s) in args.enumerate() { cs[ix] = UnsafeMutablePointer<Int8>((s as NSString).UTF8String) } var cs2 : UnsafePointer<UnsafeMutablePointer<Int8>> = UnsafePointer(cs)

Beware; cs does not contain copies. Its pointers are pointing right into the strings in args.

Recommend

  • The best way to detect any change to Internet Connection xcode swift
  • What's happened with CMutablePointer and CConstPointer in Xcode Beta3?
  • Python: BaseHTTPRequestHandler - Read raw post
  • R: use min() within dplyr::mutate()
  • Load External JavaScript File
  • Struct value of pointer, array and slice
  • WPF C# Get Root Node of TreeView
  • Passing a list of parameters into a Python function [duplicate]
  • Modification of innerHTML stored in variable not working
  • Saved Core Data does not persist after app closes 80% of the time
  • XCode 5 crash on loading the project
  • How to rename partition value in Hive?
  • flexbox sticky footer
  • Role Count using Graph Api against a tenant
  • How do I detect if an email client is configured on an Android device?
  • how to change the black color to Red with opencv python
  • device tree overlay phandle
  • Working with django : Proxy setup
  • remove date from DateTimePicker for Compact Framework
  • Firebase suddenly reports invalid signature
  • Paging Through XML Data Using jQuery and HTML
  • Issue with Terrain Collision using Three.js
  • Boolean filter using a timestamp value on a dataframe in Python
  • css: column-count 3, image floating spanning 2, chrome not playing. why?
  • Ajax call on Multiple selection in Select box
  • Google App Engine Datastore: Dealing with eventual consistency
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Write to .csv file with PHP (Commas in Data Error)
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • ReferenceError: TextEncoder is not defined