Easy array question

Member
Posts: 21
Joined: 2009.05
Post: #1
Greetings all,
I just wanted to make sure I understand the in's and out's of arrays. This should be a really easy question. Smile

Lets say I have a char array:

char name[ 10 ];

Since my array is being defined with the type of char, this array should have a null terminated end. So, how should the array look?

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ 9 ] [ NULL ]

or

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 7 ] [ 8 ] [ NULL ]

I guess my real question is: when you define an array with a number, 10, is that asking for 10 memory spots with the defined type, or 11? I get 11 because I start counting from 0, and 0 counting up to 10 is equal to 11. So, does char name[ 10 ] actually hold any data?

Well, I hope I didn't thoroughly confuse anyone. I just want to make sure I understand. Smile

Thank you for your help!

~Achi
Quote this message in a reply
sh4ggy87
Unregistered
 
Post: #2
name[9] is the last element of the array, assuming this is C or some superset. So you are allocating space for 10 chars.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
In other words, an array declared as T[n] has n slots, numbered 0 to n-1, inclusive.
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #4
Yes, this is C. Thanks guys for your help. Smile

~Achi
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #5
The null character takes up one index in your array. If you're using null terminated C Strings, then your string capacity is one less than what you allocate for.
So, if you wanted to store "text", you'd want to allocate for "text\0" or char[5];. Most of the time though, if you're dealing with strings, you'll want to allocate on the heap rather than on the stack like in the above example.
Code:
char *myString = (char*)malloc(sizeof(char) * (numberOfCharsInString + 1));
//the +1 is for the \0 terminating character.
strcpy(myString, someOtherString); //put some text in your string.

edit: Just to clarify...
When you declare "char name[10]", you're getting 10 indices staring from index 0. The null character has nothing to do with the allocation or reading of your array. The null character is part of the C string format and is a character like any other. Just like if you did "int numbers[10]" you get 10 int's and can use them all.

e.g.
Code:
char name[10];
strcpy(&name[0], "abcde");
//name now has:
// [a][b][c][d][e][NULL][x][x][x][x]
// where the [x]'s are garbage.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #6
Thanks Kelvin, Smile
I think arrays make more sense now that I know this. You've all pounded it into my head! Rasp
Quote:Most of the time though, if you're dealing with strings, you'll want to allocate on the heap rather than on the stack like in the above example.
I was just wondering why it is better to program on the heap rather than the stack. I know that the first half of the first chapter I am reading discusses a bit about the heap, though I didn't quite understand it. I'll read over that chapter in due time. Smile

Code:
strcpy(&name[0], "abcde");
Isn't &name[0] considered unprofessional? I read in my current book of reading (The Complete Reference: C Fourth Edition) that strcpy( name, "abcde" ); would be better - since the name by itself points to the first character. Well, just letting you know if you didn't. Smile Thanks for the help!

~Achi
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #7
Code:
strcpy(&name[0], "abcde");
Just a friendly reminder that name lives on the stack.
One reason it's bad to use stack memory is that when you return from your function your memory is gone. If you then try to use that memory, you're going to get a segfault or worse.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 21
Joined: 2009.05
Post: #8
Sounds good Kelvin. Smile Thanks for letting me know, I'll make sure to remember that as I program and read my book. Smile

~Achi
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Blitz: Easy Car Physics? stevejohnson 9 6,871 Dec 5, 2004 12:49 PM
Last Post: Steven