Regarding C Static/Non Static Float Arrays (Xcode, Objective C)

Posted by user1875290 on Stack Overflow See other posts from Stack Overflow or by user1875290
Published on 2012-12-04T10:31:31Z Indexed on 2012/12/04 11:07 UTC
Read the original article Hit count: 226

Filed under:
|
|
|

Basically I have a class method that returns a float array. If I return a static array I have the problem of it being too large or possibly even too small depending on the input parameter as the size of the array needed depends on the input size. If I return just a float array[arraysize] I have the size problem solved but I have other problems.

Say for example I address each element of the non-static float array individually e.g.

    NSLog(@"array[0] %f array[1] %f array[2] %f",array[0],array[1],array[2]);

It prints the correct values for the array. However if I instead use a loop e.g.

    for (int i = 0; i < 3; i++) 
 {
    NSLog(@"array[%i] %f",i,array[i]);
 }

I get some very strange numbers (apart from the last index, oddly). Why do these two things produce different results? I'm aware that its bad practice to simply return a non static float, but even so, these two means of addressing the array look the same to me.

Relevant code from class method (for non-static version)...

    float array[arraysize];

    //many lines of code later

    if (weShouldStoreValue == true) {
         array[index] = theFloat;
         index = index + 1;
     }

    //more lines of code later

    return array;

Note that it returns a (float*).

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about arrays