Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带C结构指针的Swift_C_Swift_Pointers_Struct - Fatal编程技术网

带C结构指针的Swift

带C结构指针的Swift,c,swift,pointers,struct,C,Swift,Pointers,Struct,我有一个C结构,其中包含一个指针,所有指针都在C头中定义: struct testA { int iSignal; struct testB *test; }; struct testB { int iPro; }; 然后我有一个Swift程序来初始化这个结构的一个实例: var a = testA() var b = testB() a.test = &b // this line error 但我得到了以下错误: '&' used with non-inou

我有一个C结构,其中包含一个指针,所有指针都在C头中定义:

struct testA {
  int iSignal;
  struct testB *test;
};

struct testB {
  int iPro;
};
然后我有一个Swift程序来初始化这个结构的一个实例:

var a = testA()
var b = testB()
a.test = &b // this line error
但我得到了以下错误:

'&' used with non-inout argument of type 'UnsafeMutablePointer<testB>'
“&”与类型为“UnsafeMutablePointer”的非inout参数一起使用
有人能帮忙吗?

swift文件

import Foundation

var s = testA()
s.iSignal = 1
s.test = UnsafeMutablePointer<testB>.alloc(1)  // alocate memory for one instance of testB
s.test.memory.iPro = 10

// if your testB is allocated in you C code, than just access it
let testB = UnsafeMutablePointer<testB>(s.test)
dump(testB.memory)

/*
▿ __C.testB
    - iPro: 10
*/

dump(s)

/*
▿ __C.testA
    - iSignal: 1
    ▿ test: UnsafeMutablePointer(0x1007009C0)
        - pointerValue: 4302309824

*/

dump(s.test.memory)

/*
▿ __C.testB
    - iPro: 10
*/

s.test.dealloc(1) // dont forget deallocate s.test underlying memory ! 
testc Bridgin Header.h

#include "mystruct.h"

使用Unsafemtablepointer(&b){(b)->Void in a.test=b.memory}时,我得到一个错误:无法将类型为“inout testB”的值转换为预期的参数类型“inout”\uu@Cindy检查我的更新答案。你可以看到你必须移动的方向。整个项目是三个文件,…aloc(1)alocate内存用于testB结构。你有责任释放内存!!!(见s.test.dealloc(1))
#include "mystruct.h"