Array problem.

Non-platform specific questions.

Array problem.

Postby equinox » Sat Oct 03, 2009 1:17 pm

Hi at ALL.

GLOBAL CODE:
int N[15] = {{2 3 4},{2 6 11}};

draw:

textnumber = N[0]; -> 2 3 4
textnumber = N[1]; -> 2 6 11
.....

not work. Why?

Tnk1000.
User avatar
equinox
 
Posts: 118
Joined: Tue Jan 29, 2008 3:38 pm
Score: 0 Give a positive score

Re: Array problem.

Postby DST » Sat Oct 03, 2009 2:12 pm

are you trying to store 2, 3, and 4 in that array slot, or the number 234? You can't store separate numbers in one slot, you have to use a multidimensional array

N[15][3]

for instance.

then

N[0][0] will equal 2, and N[0][1] will be 3, etc.....

because SPACE is not an integer symbol.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Array problem.

Postby Fuzzy » Sat Oct 03, 2009 2:25 pm

equinox wrote:Hi at ALL.

GLOBAL CODE:
int N[15] = {{2 3 4},{2 6 11}};


This is not a proper array. you have to fill in all 15 places and all numbers need to be separated with comma.

Code: Select all
int N[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

and it is one dimensional.

Code: Select all
int N[5][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}};


Is correct. All places must be initialized with a number. And they must follow the structure of the array.

draw:

textnumber = N[0]; -> 2 3 4
textnumber = N[1]; -> 2 6 11
.....

not work. Why?


N[0] can only store one number. 2, 3, 4 for example or 234. No spaces as DST said.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Array problem.

Postby pyrometal » Sat Oct 03, 2009 5:53 pm

Fuzzy wrote:you have to fill in all 15 places and all numbers need to be separated with comma.


Not all spaces need to be filled upon initialization, but it is recommended practice since (if inside a function), the slots that are left undefined will have random values, or if this is a global variable then everything that is undeclared is initiated to zero (most c compilers do this). Sorry Fuzzy, I felt like clarifying these few points!
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron