Uh oh... I'm being thick again :-)
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
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
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 ?
If I see it right, you mean the end of an array of char.
Am I right ?
May the force be with you
May 11, 2000 01:07 PM
Aragorn is that your name?
Or have you just been reading good old Tolkien (-:
Or have you just been reading good old Tolkien (-:
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.
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.
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.
Sorry it''s not the topic.
May the force be with you
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.
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
Nick - Head Designer, Llamasoft.net
--
Visit our website...
Llamasoft.net
Games, goodies, and ingenuity
Nick - Head Designer, Llamasoft.net--Visit our website...Llamasoft.netGames, goodies and ingenuity
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
--SR
----------------------------"To err is human... To really foul things up requires a computer.."----------------------------
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
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
If a man is talking in the forest, and there is no woman there to hear him, is he still wrong?
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
-Ironblayde
Aeon Software
"Your superior intellect is no match for our puny weapons!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement