Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fwar

Oh OK this is sad

This topic is 6922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well i''ve been programing for a long time without really knowing the structure and use of an array... its sad i know, i was wondering if someone could give a quick example of using and array with the structure layout and summarized steps of using the array in which you make.

Share this post


Link to post
Share on other sites
Advertisement
You should be able to find it out from any beginning book in c/c++. I would explain it right now, but I''m feeling really lazy today, sorry.

"Remember, I'm the monkey, and you're the cheese grater. So no messing around."
-Grand Theft Auto, London

"It's not whether I win or lose, as long as I piss you off"
-Morrigan, Super Puzzle Fighter II Turbo

Share this post


Link to post
Share on other sites
char myArr[10];

myArr[0] = ''H'';
myArr[1] = ''e'';

char *p;
p = myArr;
p++;

printf("%c",p); // displays e

There ya go

Share this post


Link to post
Share on other sites
An array is simply something programmers use to organize data better. If i do int theArray[20]; , it delcared a list of 20 integers, only they all have the same name, and are referenced using an index , which is zero-base (starting at 0). For example, the first element would be referenced by theArray[0] , the second element theArray[1] , etc. Note that when i declared the array with 20 in the index "spot", that 20 simply tells the compiler to create 20 of the integers, and since these indexes are zer-based, the hightest index you can use is x-1 , or 19 in this case.

We use arrays to make it simply to access data, especially in loops where we want to set or change large amounts of the same data. for example, if i have an array called bool Object_States[100]; that controls the states of some objects, if i want to set all the states to false, this segment would work nicely:


for(int x=0; x < 100; x++)
{
Object_States[x] = false;
}


Can you imagine what life would be like without arrays? We would need seperate variables for EVERYTHING! In this case, we would need to make 100 different variables, and set all of them to false by writing 100 lines of code that set each one to false. ACK! There are also linked-lists, which are basically used like arrays, only they can change in size (in terms of memory allocated), while arrays are constant and cant change size. So if in this case you somehow have more than 100 objects, the program wouldn''t work since there ins''t space for more than 100 objects.
I hope i explained it good!

Share this post


Link to post
Share on other sites
thanks guys, i''m going to use an array to organize bullets in a space invaders game that i''m programming, this will help alot, thanks again.

Share this post


Link to post
Share on other sites
Zipster: Just a small note. Doesn''t int myArray[20] create an array of 21 values? Try the code - you can access myArray[0] and myArray[20].

It''s ok. Everybody makes a fence-post error!

Simon Wilson
xeos digital development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
int MyArray[20] contains 20 values, 0 - 19 , just cause you can access it doesn''t mean it''s a valid thing to do and will result in a runtime error or bugs.

Share this post


Link to post
Share on other sites
The only reason you can do MyArray[20] is C does not do any bound checking, but when you do that, you run the risk of writing over other variables, and parts of you''re code. This can cause the entire system to crash,and memory will be corrupted.

Domini

Share this post


Link to post
Share on other sites
Yeah, I found that if I make a for loop, I can look at the negative array indexes (or whatever), like array[-345]. It''s just the compiler or C, and will eventually screw you over.

Yeah, I could learn on my own, but I like complaining to ya'll better.

Share this post


Link to post
Share on other sites
Yeah, I found that if I make a for loop, I can look at the negative array indexes (or whatever), like array[-345]. It''s just the compiler or C, and will eventually screw you over.

Yeah, I could learn on my own, but I like complaining to ya'll better.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!