int SomeArray[5][3] = { {1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
{13,14,15} };
does that mean that int SomeArray[0] is holding the values 1,2, and 3 and so on with the other numbers?
2d arrays
lets say there is an array:
SomeArray[0][0] (you would have to do it like that) is holding '1'.
SomeArray[0][1] is holding '2'
SomeArray[1][1] is holding '5'
SomeArray[1][2] is holding '6' ...and so on so forth.
An ASCII tetris clone... | AsciiRis
[edited by - Tiffany Smith on August 13, 2003 8:46:37 PM]
SomeArray[0][1] is holding '2'
SomeArray[1][1] is holding '5'
SomeArray[1][2] is holding '6' ...and so on so forth.
An ASCII tetris clone... | AsciiRis
[edited by - Tiffany Smith on August 13, 2003 8:46:37 PM]
In this case, SomeArray[0] is actually a pointer to a 3-int array. The compiler will usually generate a warning if you attempt to use SomeArray[0] where an int is expected. You can use it wherever an int array or pointer to int is expected. It can be a source of bugs to forget the second subscript in an array usage such as this.
So, yes, in a sense SomeArray[0] IS holding the numbers 1, 2, and 3. SomeArray[1] would be holding 4, 5, and 6. But something like printf("%d",SomeArray[0]) will probably result in pointer value gibberish, and not the values of the numbers.
Josh
vertexnormal AT linuxmail DOT org
Check out Golem at:
My cheapass website
So, yes, in a sense SomeArray[0] IS holding the numbers 1, 2, and 3. SomeArray[1] would be holding 4, 5, and 6. But something like printf("%d",SomeArray[0]) will probably result in pointer value gibberish, and not the values of the numbers.
Josh
vertexnormal AT linuxmail DOT org
Check out Golem at:
My cheapass website
quote:Original post by VertexNormal
In this case, SomeArray[0] is actually a pointer to a 3-int array.
nope, not for 2d static arrays. its allocated as one chunk.
what would SomeArray[1][0] be holding?
tiffany can you please explain to me Why those 2d arrays hold the values they do? or anyone?
so
SomeArray[0][0] (you would have to do it like that) is holding '1'.
SomeArray[0][1] is holding '2'
SomeArray[1][1] is holding '5'
SomeArray[1][2] is holding '6' ...and so on so forth.
why do those things hold the things that they do? i think the thing that I'm not understanding is how the array values are accessed
[edited by - TheOne1 on August 13, 2003 9:03:10 PM]
tiffany can you please explain to me Why those 2d arrays hold the values they do? or anyone?
so
SomeArray[0][0] (you would have to do it like that) is holding '1'.
SomeArray[0][1] is holding '2'
SomeArray[1][1] is holding '5'
SomeArray[1][2] is holding '6' ...and so on so forth.
why do those things hold the things that they do? i think the thing that I'm not understanding is how the array values are accessed
[edited by - TheOne1 on August 13, 2003 9:03:10 PM]
quote:Original post by TheOne1
what would SomeArray[1][0] be holding?
tiffany can you please explain to me Why those 2d arrays hold the values they do? or anyone?
so
SomeArray[0][0] (you would have to do it like that) is holding '1'.
SomeArray[0][1] is holding '2'
SomeArray[1][1] is holding '5'
SomeArray[1][2] is holding '6' ...and so on so forth.
why do those things hold the things that they do? i think the thing that I'm not understanding is how the array values are accessed
SomeArray[1][0] would be holding 4
there is a really easy way to figure it out...
here ...
#include <stdio.h>#include <conio.h>int main() {int array[5][3] ={ {1,2,3},{4,5,6},{7,8,9}, {10,11,12}, {13,14,15} }; printf("%d", (array[1][0]));getch(); return 0;}
Compile this and play around with the values in the printf.
i think the really confusing part about arrays is that all array subscripts start with a 0, imo thats all that makes it confusing.
int SomeArray[5][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}, {13,14,15} };
this part {1,2,3} is SomeArray[0] because its the first array of your five.
SomeArray[0][0] would print 1 because 1 is the fisrt number in your first array of three.
{4,5,6} is SomeArray[1] because its the second array of your five.
SomeArray[1][0] would print 4 because 4 is the first number in your second array of three.
does that make sense?
An ASCII tetris clone... | AsciiRis
[edited by - Tiffany Smith on August 13, 2003 9:29:32 PM]
quote:Original post by Anonymous Posterquote:Original post by VertexNormal
In this case, SomeArray[0] is actually a pointer to a 3-int array.
nope, not for 2d static arrays. its allocated as one chunk.
int SomeArray[5][3]={ {1,2,3}, {4,5,6},{7,8,9},{10,11,12},{13,14,15}}; printf("Somearray[0]: %d\n", SomeArray[0] );
Messages upon compilation:
main.cpp: In function 'int main(int, char**)'
main.cpp:210:warning: int format, pointer arg (arg 2)
I know it is allocated as one chunk, but conceptually, SomeArray[0] is a pointer to a 3-int array, and can be used wherever a pointer to a 3-int array is expected. Suffice it to say that the compiler does not like left-out subscripts.
Josh
vertexnormal AT linuxmail DOT org
Check out Golem at:
My cheapass website
[edited by - VertexNormal on August 13, 2003 9:32:22 PM]
Thank you Tiffany! I now see the light!
edit:
but i might have some questions later on, thanks for your help
[edited by - TheOne1 on August 13, 2003 10:36:23 PM]
edit:
but i might have some questions later on, thanks for your help
[edited by - TheOne1 on August 13, 2003 10:36:23 PM]
array notation in C is simply shorthand for pointer arithmatic ...
here is a pointer I will use to access a dynamically allocated chunk of memory ...
int *myBlock = new int[9];
and here is a pointer to access an array
int myArray[9];
these notations are equal:
*(myBlock) and myArray[0]
these are also equal
*(myBlock + 5) and myArray[5]
and
*(myBlock + 10) and myArray[10]
starting to see a connection ...
now let''s look at 2 dimensional arrays ...
int myArray2D[3][3];
now all 3 of my blocks have enough memory to hold 9 ints ... and here is how the 2D array acts in terms of pointer arithmatic and 1D array symantics ...
myArray2D[1][2] is like myArray[1 * 3 + 2] and *(myBlock + (1 * 3 + 2))
another example ...
myArray2D[3][1] is like myArray[3 * 3 + 1] and *(myBlock + (3 * 3 + 1))
...
here is the general case for 2d arrays
if the array is created as array[X][Y] ..
and accessed as array<A href='http://<b>
then the pointer arithmatic is *(array + (A * Y + B))
if it is a 3d array it would be
created as array[x][y][z]
accessed as array[a][c]<br><br>final result is *(array + (a * y * z + b * z + c))<br><br>i realize my explanation is kinda sloppy … sorry </b> ' Target=_Blank>Link</a>
here is a pointer I will use to access a dynamically allocated chunk of memory ...
int *myBlock = new int[9];
and here is a pointer to access an array
int myArray[9];
these notations are equal:
*(myBlock) and myArray[0]
these are also equal
*(myBlock + 5) and myArray[5]
and
*(myBlock + 10) and myArray[10]
starting to see a connection ...
now let''s look at 2 dimensional arrays ...
int myArray2D[3][3];
now all 3 of my blocks have enough memory to hold 9 ints ... and here is how the 2D array acts in terms of pointer arithmatic and 1D array symantics ...
myArray2D[1][2] is like myArray[1 * 3 + 2] and *(myBlock + (1 * 3 + 2))
another example ...
myArray2D[3][1] is like myArray[3 * 3 + 1] and *(myBlock + (3 * 3 + 1))
...
here is the general case for 2d arrays
if the array is created as array[X][Y] ..
and accessed as array<A href='http://<b>
then the pointer arithmatic is *(array + (A * Y + B))
if it is a 3d array it would be
created as array[x][y][z]
accessed as array[a][c]<br><br>final result is *(array + (a * y * z + b * z + c))<br><br>i realize my explanation is kinda sloppy … sorry </b> ' Target=_Blank>Link</a>
quote:Original post by TheOne1
Thank you Tiffany! I now see the light!
edit:
but i might have some questions later on, thanks for your help
Glad to have helped.
An ASCII tetris clone... | AsciiRis
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement