从Objective-C调用C函数失去了价值

问题描述:

我想保持我的表行高的float []。我在这里找到了一个很好的课程:http://forums.macnn.com/t/224809/nsmutablearray-vs-a-plain-c-array-for-storing-floats从Objective-C调用C函数失去了价值

问题是,一旦C函数(位于另一个文件中)被调用,我传入的float变为0.每次都会发生这种情况,无论float值如何。

C函数:

typedef struct 
{ 
    float *array; 
    int count; 
} floatArray; 

BOOL AddFloatToArray (floatArray *farray, float newFloat) 
{ 
    if (farray->count > 0) 
    { 
     // The array is already allocated, just enlarge it by one 
     farray->array = realloc (farray->array, ((farray->count + 1) * sizeof (float))); 

     // If there was an error, return NO 
     if (farray->array == NULL) 
      return NO; 
    } 
    else 
    { 
     // Allocate new array with the capacity for one float 
     farray->array = (float *)malloc (sizeof (float)); 

     // If there was an error, return NO 
     if (farray->array == NULL) 
      return NO; 
    } 
    printf("Adding float to array %f\n", newFloat); 
    farray->array[farray->count] = newFloat; 
    farray->count += 1; 
    printArrayContents(farray); 
    return YES; 
} 

int printArrayContents(floatArray* farray) 
{ 
    printf("Printing array contents\n"); 
    for(int j=0; j < farray->count; j++) 
     printf("%f\n", farray->array[j]); 


    return 0; 
} 

从名为:

NSDictionary* review = [self.reviews objectAtIndex:indexPath.row]; 
    returnHeight = [ReviewCell cellHeightForReview:review]; 
    NSLog(@"Adding height to array: %0.0f", returnHeight); 
    AddFloatToArray(heights, returnHeight); 

这里记录的内容:

2012-09-28 11:46:12.787 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 101 
Adding float to array 0.000000 
Printing array contents 
0.000000 
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 138 
Adding float to array 0.000000 
Printing array contents 
0.000000 
0.000000 
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 122 
Adding float to array 0.000000 
Printing array contents 
0.000000 
0.000000 
0.000000 
2012-09-28 11:46:12.789 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 139 
Adding float to array 0.000000 
Printing array contents 
0.000000 
0.000000 
0.000000 

我怎样才能确保正确的价值实际上是插入浮动[]?

+0

尝试完全按照您发布的代码并使用'CGFloat returnHeight = 120.f; AddFloatToArray(heights,returnHeight);'和我的Xcode中的魅力一样。当你在这里c/p时,你确定你没有从你的代码中剥离任何东西吗? – AliSoftware

+0

一切都在这里。可能值得一提的是C函数在不同的文件中。 – MishieMoo

我没有在.h中声明函数,我是#importing。所以一切都编译和运行,但类没有正确通信。你会认为这会引发警告或错误。

+2

它应该触发警告“隐式声明函数xxx在C99中无效”。检查是否已启用'-Wimplicit-function-declaration'标志。 – AliSoftware

+0

这没有打开,但我记得之前看到过这个警告。谷歌虽然没有提出这个解决方案。谢谢! – MishieMoo

+2

我也有时会看到“xxx未定义,假设extern返回int”警告,可能是相关的(不知道函数签名,因为你没有导入头文件,编译器假定函数返回了一个'int',而不是'float',并且函数调用过程中的参数堆栈会因此而混乱)。但是我无法设法再现这个警告,也找不到相应的'-W'标志,这在这种情况下会有所帮助。 – AliSoftware