|
-
September 17th, 2010, 05:45 AM
#1
question about two dimensional arrays
Hi to all,
Hope you all will be fine.I am confused about Arrays . Consider this, it's a single dimensional integer array
Code:
int product_codes[3] = {10, 972, 45};
Also character array
Code:
char name[] = {'S', 'a', 'm', '\0'};
See, For initialize single dimensional array we use one set of curly({ }) braces and separate each character with comma(,).
But what about this
Code:
char Ones[10][10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};
See, in this we also use singe set of curly braces and separate each character with comma(,), but it's not a single dimension, it's a two dimensional array. How and why?
If we declare it like this i get an error
Code:
char Ones[10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"}; //Error
Can someone please explain to me why we declare it with two dimension and why i getting error while creating it with single dimension.
It's a two dimension array
Code:
int matrix[2][4] ={ {1, 2, 3, 4}, {10, 20, 30, 40} };
We used two set of curly braces inside a single set of curly braces to initialize two dimensional array but we didn't initialize {"One", "Two",....} array in this manner why?
Thanks
-
September 17th, 2010, 06:04 AM
#2
Re: question about two dimensional arrays
char Ones[10][10] = {"One", "Two", ...
'O' 'n' 'e' '/0' == 4 * char
'T' 'w' 'o' '/0' == 4 * char
Each comma is separating an array of characters.
char name[] = {'S', 'a', 'm', '\0'};
could be written as
Code:
char name[] = "Sam" ;
char name[10] = "Sam" ;
your humble savant
-
September 17th, 2010, 09:31 AM
#3
Re: question about two dimensional arrays
Hi,
Thanks. You mean the code
Code:
char Ones[10][10] = {"One", "Two","Three","Four","Five","Six","Seven","Eight","Nine"};
is equivalent to
Code:
char Ones[10][10] ={{'O', 'n', 'e', '\0'}, {'T', 'w', 'o', '/0'}, {'T', 'h', 'r', 'e', 'e', \0'}, .....};
Means each string("One", "Two",...) is itself a separate character array separated by commas as i did above.
Thanks
-
September 17th, 2010, 09:39 AM
#4
Re: question about two dimensional arrays
Effectively yes, a string literal is a special form of char array.
The primary difference between the {'a', '\0'} notation and the "a" notation is that while the former may only be used in initialization, the latter may be used anywhere:
Code:
char str[] = {'a', '\0'};
strcpy(str, "b");
Also, it is not necessary to copy string literals into a variable; you can simple point at their location in (probably read-only) memory instead. But better make sure you do so in a const manner or you risk ugly problems:
Code:
const int *i = {1,2,3}; // NOT possible
int i2[] = {1,2,3}; // just fine, the array is copied to the stack
const char *s = "Hello"; // just fine, so long as s is a const char*
char *s2 = "World"; // the compiler will accept this, but it's a bad idea because attempting to modify the string literal "in-place" without copying it to the stack will likely crash your program
char s3[] = "Test"; // just fine, the literal is copied to the stack
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|