#import <Foundation/Foundation.h>@interface SampleClass:NSObject
-(void) getSeconds:(int*)par;@end@implementation SampleClass
-(void) getSeconds:(int*)par {/* get the current number of seconds */*par =time(NULL);return;}@endintmain(){int sec;
SampleClass *sampleClass =[[SampleClass alloc]init];[sampleClass getSeconds:&sec];/* print the actual value */NSLog(@"Number of seconds: %d\n", sec );return0;}
2020-08-08 10:40:03.005 test[11928:4136] Number of seconds: 1596854402
该函数可以接受一个指针,也可以接受一个数组,如以下示例所示:
@interface SampleClass:NSObject
/* function declaration */-(double) getAverage:(int*)arr ofSize:(int) size;@end@implementation SampleClass
-(double) getAverage:(int*)arr ofSize:(int) size {int i, sum =0;double avg;for(i =0; i < size;++i){
sum += arr[i];}
avg =(double)sum / size;return avg;}@endintmain(){/* an int array with 5 elements */int balance[5]={1000,2,3,17,50};double avg;
SampleClass *sampleClass =[[SampleClass alloc]init];/* pass pointer to the array as an argument */
avg =[sampleClass getAverage: balance ofSize:5];/* output the returned value */NSLog(@"Average value is: %f\n", avg );return0;}
2020-08-08 10:42:37.483 test[6612:332] Average value is: 214.400000