Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

paulina7m

macrumors newbie
Original poster
Sep 26, 2007
16
0
Ottawa, Canada
Hi,
how come in my *.mm file, I cannot do this:

float*data[1000][4][4];

data[0][0] = {0.0f, 0.4f, 0.1f, 0.5f};
(here I get errors about curly braces being in the wrong place)
??


I also tried

float * data[100][4][4] = {0};
float * data[100][4][4] = {nil};
float * data[100][4][4] = {NULL};

They all give errors.

What's the proper way to do this then?
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
You're declaring a pointer to a 1000x4x4 array of floats. To declare a 1000x4x4 array of floats, just remove the asterisk:

float data[1000][4][4] = {{{0}}};

This should work fine. Note the three sets of braces around the zero; you should use one set of braces for each dimension or else you'll get a warning.

Also note that you can only use curly braces to initialize your array; they're only valid when you first declare your variable. You can't use curly braces to assign values to an array or element after it's been declared. The example you gave above:

data[0][0] = {0.0f, 0.4f, 0.1f, 0.5f};

is not valid.
 

paulina7m

macrumors newbie
Original poster
Sep 26, 2007
16
0
Ottawa, Canada
ah!!! great, this works perfectly! thank you!

Oh, and is there a differemce if I use 0, or NULL, or nil, if I'm using .mm?

Later, I'd be checking, if the value is not nil.
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
ah!!! great, this works perfectly! thank you!

Oh, and is there a differemce if I use 0, or NULL, or nil, if I'm using .mm?

Later, I'd be checking, if the value is not nil.

0, NULL, and nil all represent the value zero. However, 0 has type int, NULL has type void*, and nil has type id. (NULL and nil represent memory address zero) If you want to declare an array of floats with each element set to zero, use {{{0}}}. {{{0.0f}}} is technically more correct, but the compiler will take care of the cast for you.

Also, what do you mean by "checking if the value is not nil?" Since your array was declared on the stack it will never be nil.
 

paulina7m

macrumors newbie
Original poster
Sep 26, 2007
16
0
Ottawa, Canada
Well, coming from other languages,
I'd first initialize all values to NULL.
Then assign some values, like first 10 entries, for example.
The rest would be still NULL.

Then i would use the while array != NULL,
to loop through the array.

I wanted to do smth similar to this.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
If you're using .mm, you should have access to dynamic data structures from C++ like vector or from Obj-C like NSMutableArray, and you'll have things like length to loop over. If you're set on using C-style arrays, you can initialize large tracts of memory with memset. Any special reason for this approach?

-Lee
 

paulina7m

macrumors newbie
Original poster
Sep 26, 2007
16
0
Ottawa, Canada
1. i tried using vector, but somehow i kept getting an error that
#include <vector>
is not declared anywhere.
I also was including<stdlib.h> and <stdio.h> before that.

2. I decided to use straight c arrays for performance reasons.

3. couldn;t figure out how to make 3d vectors:)

anyway,
float data[1000][4][4] is working.

And it took me a while to figure out that I had to use float (*)[4][4] for return in my getter for data array.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.