0

I want to create an array from a function. The following obviously works, however because my arrays are much larger I want to save the space and the time writing them out.

GLfloat gCubeTextureData_floorj[2] = { 1.0, 1.0 };

In my attempts to create an array from a function, I am getting the error that 'Array initializer must be an initializer list'. However I am initializing the array by returning the list. This is my function call:

 GLfloat array[2] = [self createCubeTextureFromX1:0.5f toX2:1.0f toY1:0.0f andY2:0.5f];

I have tried the following for the function:

-(GLfloat[2]) createCubeTextureFromX1:(float)x1 toX2:(float)x2 toY1:(float)y1 andY2:(float)y2 {
    GLfloat gCubeTextureData_floorj[2] = { 1.0, 1.0 };
    return gCubeTextureData_floorj;
}

and I have tried

-(GLfloat[2]) createCubeTextureFromX1:(float)x1 toX2:(float)x2 toY1:(float)y1 andY2:(float)y2 {
    return { 1.0, 1.0 };
}

and both of these don't work. The first gives me the error in the function, the second gives me the error on the function call. Is this possible to do?

2
  • what language are you using then? you have three languages tagged Commented May 15, 2013 at 18:07
  • My compiler is set to Objective-C++, so I can use any of the three languages. However, I would prefer to use Objective-C (or Objective-C++) as you can see from my function name. Commented May 15, 2013 at 18:15

3 Answers 3

1

In C, arrays are typically returned using pointers.

To do this properly in Objective-C, I'd suggest returning a NSArray from your method.

However, GLFloats aren't native Objective-C objects that can be stored directory in NSArray. To get around this, I'd recommend using NSNumber or NSValue objects to contain your GLfloat values.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem with using NSArray and NSNumber is I need to then use the values in glBufferData which I wouldn't know how to do. (OpenGL ES program)
Whatever you insert into an NSMutableArray going in, you can extract from the (mutable or immutable) NSArray on the other end via "objectAtIndex:". Once you get comfortable with Objective-C arrays it'll become easier.
1

It is not possible to initialize a C array from a function, method, $whatever. It can be only initialized with an initializer list. ISO/IEC 9899:TC3, § 6.7.8, 16. "Otherwise, the initializer for an object that has aggregate or union type shall be a brace- enclosed list of initializers for the elements or named members."

What you can do:

Instead of the C array use a pointer. Heap alloc an array in the creation function or method and return the pointer of the array. The caller is responsible for freeing it.

1 Comment

Ah, an addition: IIRC in C++ you can assign arrays. But my knowledge of C++ is that old, that I do not know the details.
0

Why not a C99 compound literal?

- (float[2])fooX:(float)x andY:(float)y
{
    return (float[2]){ x, y };
}

8 Comments

That still gives the 'Array initializer must be an initializer list' eror
@michaellindahl No, it doesn't.
It does when initializing a C array.
@AminNegm-Awad Platform, compiler, compiler flags, etc...?
Mac OS – clang/LLVM – standard You assign the retval to a pointer, not to an array. This is, what I wanted to say in my answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.