PDA

View Full Version : Defining Array




Lumio
Sep 3, 2009, 08:11 AM
Hi!
I want to define a special array. Right now it looks like this:
int translation[] = {
1,
2
};

But I would like to have it like this, so that the indexes are fixed:
int translation[] = {
12 = 1,
14 = 2
};

How to do that?

Greetings :)



lee1210
Sep 3, 2009, 08:19 AM
Someone may need to correct me on this, but i don't think this is possible. It seems easiest to just do:
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

brn2ski00
Sep 3, 2009, 08:28 AM
Someone may need to correct me on this, but i don't think this is possible. It seems easiest to just do:
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

that should work just fine.

int[] array = new int[]; array[12] = 1; array[14] = 2

chown33
Sep 3, 2009, 11:53 AM
Hi!
I want to define a special array. Right now it looks like this:
int translation[] = {
1,
2
};

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.

gnasher729
Sep 3, 2009, 04:01 PM
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.