Defining Array

Discussion in 'Mac Programming' started by Lumio, Sep 3, 2009.

  1. Lumio macrumors member

    Joined:
    May 20, 2008
    #1
    Hi!
    I want to define a special array. Right now it looks like this:
    Code:
    int translation[] = {
    		1,
    		2
    };
    But I would like to have it like this, so that the indexes are fixed:
    Code:
    int translation[] = {
    		12 = 1,
    		14 = 2
    };
    How to do that?

    Greetings :)
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Someone may need to correct me on this, but i don't think this is possible. It seems easiest to just do:
    Code:
    int translation[15];
    translation[12] = 1;
    translation[14] = 2;
    Otherwise, you'd need to fill in the spaces with 0. Probably better to clear the memory anyway.

    -Lee
     
  3. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #3
    that should work just fine.

    Code:
    int[] array = new int[]; array[12] = 1; array[14] = 2
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Which programming language do you want the answer in? Your source looks valid in C, Objective-C, and Java, depending on context. It may also be valid in other languages.
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    Code:
    	int translation [] = { [12] = 1, [14] = 2 };
    
    Type "c99 standard draft" into Google the find the latest draft of the C99 Standard; Objective C is also based on that standard.
     

Share This Page