2010-07-21 47 views
2

我正在使用Objective-C语言。但我不知道如何在Objective-C中使用c。如何使用C语言数组与Objective-C

Ex)这是函数方法。

- (??) function{ 
unsigned int first = .. 
unsigned int second = .. 
unsigned int third = .. 

int infoData = {first,second,third}; 
return infoData; 
} 

如何填写括号。

我不使用NSArray。

请帮帮我。

回答

3

的严格的超假设你声明int[] infoData你可以做回int*,但你还是会遇到问题,因为该数组在函数的堆栈上分配。你需要动态分配空间,它就像你会在C.

(不能使用int[]作为返回类型)

下面的代码可以编译,但GCC将警告的地址返回一个函数局部变量。

@interface test 
- (int*) function; 
@end 

@implementation test 

- (int*) function{ 
    unsigned int first = 0; 
    unsigned int second = 1; 
    unsigned int third = 2; 

    int infoData[] = {first,second,third}; 
    return infoData; 
} 

@end 
4

的答案是一样的,因为它是在C的Objective-C是C.