Using curly braces to initialize and array, or to assign to it

Discussion in 'Mac Programming' started by paulina7m, Dec 3, 2008.

  1. paulina7m macrumors newbie

    paulina7m

    Joined:
    Sep 26, 2007
    Location:
    Ottawa, Canada
    #1
    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?
     
  2. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #2
    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.
     
  3. paulina7m thread starter macrumors newbie

    paulina7m

    Joined:
    Sep 26, 2007
    Location:
    Ottawa, Canada
    #3
    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.
     
  4. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #4
    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.
     
  5. paulina7m thread starter macrumors newbie

    paulina7m

    Joined:
    Sep 26, 2007
    Location:
    Ottawa, Canada
    #5
    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.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. paulina7m thread starter macrumors newbie

    paulina7m

    Joined:
    Sep 26, 2007
    Location:
    Ottawa, Canada
    #7
    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.
     

Share This Page