C语言 在目标 C 中使用 C 结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2172887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use C Struct in Objective C
提问by paul simmons
In an Xcode project I have a C file with functions, it compiles and works OK
在 Xcode 项目中,我有一个带有函数的 C 文件,它可以编译并正常工作
I want to wrap my C code in struct(s), how will I be able to call them in Objective-C?
我想将我的 C 代码包装在 struct(s) 中,我将如何在 Objective-C 中调用它们?
回答by Luca Matteis
Objective-C is a proper superset of C. Anything you can do in C can be done identically in Objective-C. So, you really don't need to think of them as different languages; Objective-C is simply "C plus some more stuff".
Objective-C 是 C 的适当超集。您在 C 中可以做的任何事情都可以在 Objective-C 中完成。因此,您真的不需要将它们视为不同的语言;Objective-C 只是“C 加上一些更多的东西”。
// this struct is compatible with C and Obj-C
struct fruit {
int a;
};
int main()
{
struct fruit apple;
apple.a = 1;
return 0;
}
Then, any C or Objective-C source file can access that struct. There aren't any additional complications introduced by Objective-C.
然后,任何 C 或 Objective-C 源文件都可以访问该结构。Objective-C 没有引入任何额外的复杂性。
回答by stefanB
Declare function pointers, add them to your structure and then call them, it's just C.
声明函数指针,将它们添加到您的结构中然后调用它们,它只是 C。
Example:
例子:
//Typedef 2 function pointers, first takes and returns int,
// second takes and returns double
typedef int (*FuncPtrInt) (int);
typedef double (*FuncPtrDouble)(double);
// create structure to store function pointers
struct ABC
{
FuncPtrInt applyA;
FuncPtrDouble applyB;
};
// create some functions to use with structure
int incrFuncA(int num) { return ++num; }
double decrFuncB(double num) { return --num; }
double multiplyFuncB(double num) { return num*num; }
// try it out
void testStruct()
{
struct ABC abc;
abc.applyA = incrFuncA;
abc.applyB = decrFuncB;
NSLog(@"increment: %d",abc.applyA(3));
NSLog(@"decrement: %f",abc.applyB(3.5));
abc.applyB = multiplyFuncB;
NSLog(@"multiply: %f",abc.applyB(3.5));
}
Output:
输出:
2010-02-01 10:36:22.335 x[11847] increment: 4
2010-02-01 10:36:22.336 x[11847] decrement: 2.500000
2010-02-01 10:36:22.336 x[11847] multiply: 12.250000
If you want to have a struct with functions where functions operate on the structure you have to pass the pointer to that function by default (similar to what c++ does):
如果你想要一个带有函数的结构体,其中函数在结构体上运行,你必须默认将指针传递给该函数(类似于 c++ 所做的):
Define:
定义:
struct ClassABC;
typedef int (*FuncPtrClassABC)(struct ClassABC *);
typedef int (*FuncPtrClassABCInt)(struct ClassABC *, int);
int incrFunc(struct ClassABC * abc);
int decrFunc(struct ClassABC * abc);
int addFunc(struct ClassABC * abc, int num);
int subtractFunc(struct ClassABC * abc, int num);
struct ClassABC
{
int i;
FuncPtrClassABC increment;
FuncPtrClassABC decrement;
FuncPtrClassABCInt add;
FuncPtrClassABCInt subtract;
};
As you can see these functions could be standalone, you would still pass the ClassABC in:
正如您所看到的,这些函数可以是独立的,您仍然需要传入 ClassABC:
int incrFunc(struct ClassABC * abc) { return ++(abc->i); }
int decrFunc(struct ClassABC * abc) { return --(abc->i); }
int addFunc(struct ClassABC * abc, int num)
{ abc->i += num; return abc->i; }
int subtractFunc(struct ClassABC * abc, int num)
{ abc->i -= num; return abc->i; }
Initialization helper func:
初始化辅助函数:
void initClassABC(struct ClassABC * abc)
{
abc->i = 0;
abc->increment = incrFunc;
abc->decrement = decrFunc;
abc->add = addFunc;
abc->subtract = subtractFunc;
}
Usage:
用法:
struct ClassABC cabc;
initClassABC(&cabc);
cabc.add(&cabc,4);
NSLog(@"add: %d", cabc.i);
cabc.decrement(&cabc);
NSLog(@"decrement: %d", cabc.i);
cabc.subtract(&cabc,2);
NSLog(@"subtract: %d", cabc.i);
Output:
输出:
2010-02-01 10:56:39.569 x[12894] add: 4
2010-02-01 10:56:39.569 x[12894] decrement: 3
2010-02-01 10:56:39.569 x[12894] subtract: 1
Enjoy
享受

