Jump to content
  • Advertisement

Archived

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

Llamasoft.net

Uh oh... I'm being thick again :-)

This topic is 6946 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

Hey guys, Here I come with another not-so-hot question, I know it''s simple, just bear with me. In an array, say: int The_array[10]; Can I use elements 0 - 9? i.e use all ten slots? I''m sure I can, but I needed to check, because I heard somewhere sometime that you needed to leave the last element free for a null termination character... or something Please anyone just tell me i''m right, or if necessary, i''m wrong Nick - Head Designer, Llamasoft.net -- Visit our website... Llamasoft.net Games, goodies, and ingenuity

Share this post


Link to post
Share on other sites
Advertisement
As far as I know you can use 0-9.
If I see it right, you mean the end of an array of char.
Am I right ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Aragorn is that your name?
Or have you just been reading good old Tolkien (-:

Share this post


Link to post
Share on other sites
It''s looks like to me that you are mixing up a few things.

First of all:

int The_array[10];

will give you an array of ten integers (0-9).

As far as leaving a character for "\n", I''m pretty sure that only applies to strings, say you were doing something like.

char The_array[10];

You could store 9 characters followed by the null terminator.

P.S. I haven''t done much C/C++ since the DOS days, so if i''m wrong please correct me, but I''m pretty sure I''m correct on this.

Share this post


Link to post
Share on other sites
Yes, it''s that Aragorn. He is the character I like most (beside Gandalf, but this was taken already).

Sorry it''s not the topic.

Share this post


Link to post
Share on other sites
I am pretty sure the other guys are right. I recently moved from dos to windows programming, so things are pretty fresh in my mind.

Share this post


Link to post
Share on other sites
borngamer: Yeah that''s exactly what I touhght it was, just for strings... thanx alot I Just got a bit mixed up and thought who better to ask than you guys !

Nick - Head Designer, Llamasoft.net

--
Visit our website...

Llamasoft.net
Games, goodies, and ingenuity

Share this post


Link to post
Share on other sites
Actually inarray[10]; will give you 11 elements... That''s 0-10.. As for a null character at the end, that should only apply to strings (even then in special cases) or if you are doing some really starange cool method of data organizing.. =)

--SR

Share this post


Link to post
Share on other sites
Regardless of what you are storing in the array (ints, chars, doubles, ...etc.) you may store ten- (10) values. You may index these values using 0 through 9. If you wish to use the string library functions on an array of characters (ie. strlen(), strcmp(), strcpy(), ...etc.) then you must "terminate the string" with a NULL character. This means that if you fill the first 3 positions of the array with chars, you must populate the fourth position with NULL. Ultimately it means that of the ten postions, one must be reserved for a terminating NULL character. If you do not intend to use the str functions (or if you only intend to use functions like strncmp(), strncpy(), ...etc.) you do not need to terminate the string. Just be aware that virtually any standard C library function which takes a string as a parameter requires that it be NULL terminated...

PS: SmacknRat... I'm sorry my friend, you are dead wrong.

int The_array[10];

will give you 10, not 11 ints.

Edited by - novalis on May 11, 2000 2:34:39 PM

Share this post


Link to post
Share on other sites
Smacknrat, you don''t get 11 positions in the array, you get 10. Only inarray[0] through inarray[9] are allocated to the array. If you try to access inarray[10] in your program, the compiler will let you because it doesn''t perform bounds checking on arrays. But since that value isn''t allocated to the array, you are likely to overwrite space in memory that another variable is supposed to be using.

-Ironblayde
Aeon Software

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!