2015-10-29 42 views
0

我想使字符的交流風格數組指針這樣的:如何decleare char * const的的argv []中迅速

*const argv[] 

我想通了,我必須使用UnsafePointer<UnsafeMutablePointer<Int8>>

但我不不知道如何初始化它。

我該如何映射這種正常的陣列來UnsafePointer<UnsafeMutablePointer<Int8>>

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

謝謝

+0

這是無效的* const argv []',你想要做什麼? –

+0

@iharob運行此功能==> public func execvp(_:UnsafePointer ,_:UnsafePointer >) - > Int32 – DEopen

+0

@iharob execvp(const char * file,char * const argv []); – DEopen

回答

2

最簡單的辦法是讓可可形成你的C字符串:

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) 

當心; cs不包含副本。它的指針指向args中的字符串。

+0

謝謝@馬特,但我需要UnsafePointer >不是UnsafeMutablePointer >。 UnsafePointer沒有alloc,UnsafeMutablePointer有。你能解釋一下如何用這種方法制作一個UnsafePointer >嗎? – DEopen

+1

'let cs2:UnsafePointer > = UnsafePointer(cs)' – matt

+0

爲什麼要讓內部指針變爲可變?在C字符串中,它們不是。 – matt