View Full Version : loading array values directly
jamadagni
7th January 2006, 16:44
I want to load 9 predetermined strings and 8 predetermined integers to two different arrays. I have seen code of the form:
char *namelist[] = {"Ram", "Shyam", "Dhyaan",};
int numlist[] = {0, 1, 4, 2, 5, 3, 6, 11,};
But I want to know some things:
what is the * for in the first statement?
since a string is already a character array and I need to create an array of strings, don't I need to write char *namelist[][] (two []-s)?
is the comma necessary before the closing brace?
is the semicolon necessary after the closing brace?
jacek
7th January 2006, 17:18
what is the * for in the first statement?
namelist is an array of strings and strings in C are of char * type.
don't I need to write char *namelist[][] (two []-s)?
No, futhermore you can't declare a multidimentional array without specifying dimentions.
int wont_work[][] = { {1,2,3}, {4,5,6} }; // error
int will_work[][3] = { {1,2,3}, {4,5,6} };
is the comma necessary before the closing brace?
No.
is the semicolon necessary after the closing brace?
Yes.
jamadagni
8th January 2006, 02:31
strings in C are of char * type.
Do I read that as "strings in C are of the nature of pointers to char-length memory spaces"?
futhermore you can't declare a multidimentional array without specifying dimentions.
int wont_work[][] = { {1,2,3}, {4,5,6} }; // error
int will_work[][3] = { {1,2,3}, {4,5,6} };
How come you still don't need to specify the first dimension? And if I declare char *namelist[] and include lots of strings should it not be mandatory that I specify the second dimension - i.e. the maximum length of strings?
jacek
8th January 2006, 16:08
Do I read that as "strings in C are of the nature of pointers to char-length memory spaces"?
Yes.
How come you still don't need to specify the first dimension?
It's one of those inconsistencies and in fact arrays are evil (http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1).
And if I declare char *namelist[] and include lots of strings should it not be mandatory that I specify the second dimension - i.e. the maximum length of strings?
Because you declare an array of pointers, not a two dimensional array.
wysota
8th January 2006, 18:38
It's one of those inconsistencies and in fact arrays are evil (http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1).
Isn't that because you provide the dimension of the array right in the assignment? And furthermore, is that a standard or a compiler extension to allow such a declaration and assignment statement?
jacek
8th January 2006, 18:50
is that a standard or a compiler extension to allow such a declaration and assignment statement?
K&R use that form of assignment in their book.
Powered by vBulletin® Version 4.2.5 Copyright © 2023 vBulletin Solutions Inc. All rights reserved.