The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I need an additional alias/reference to a multidimensional character array. I'm able to copy the first array with, char *A1 = A[0]; but when I try to apply that logic to higher dimensions like char **A1 = A; it doesn't compile. A[12][12] is just an array of pointers, right? Why isn't this working?
admanbunionize your workplaceSeattle, WARegistered Userregular
edited April 2010
C++ was built on C and C was built on speed. Since arrays are declared at a constant size it's much faster to allocate one giant chunk of memory for a mulitdimensional array and access values by basic arithmetic. i.e. for an array declared A[n][m] you access element a[j] with the formula:
I need an additional alias/reference to a multidimensional character array. I'm able to copy the first array with, char *A1 = A[0]; but when I try to apply that logic to higher dimensions like char **A1 = A; it doesn't compile. A[12][12] is just an array of pointers, right? Why isn't this working?
char* A1 = A[0] doesn't copy the array at all. Also, what is your declaration for A here? What you're posting is fairly jumbled.
Lets say that you do the following:
int A[5] = {10, 11, 12, 13, 14};
int* B = A;
Then B is just a pointer to the location of memory where the first element of A is stored. So if you dereferenced B directly you would get 10, but B is not a copy of A. If you changed the data in A, what you would get out of dereferencing B would be different as well, and the same would be true if you changed the array by accessing it through B. Because they both would point to the same place where the underlying data resides.
You need to be careful when reading what admanb is saying, because some of the stuff he is talking about isn't in C++ syntax but rather is referring to the concept of having a matrix stored in a one dimensional array rather than a multidimensional array.
Multidimensional arrays in C++ are arrays of arrays, and it is possible to make them such that they have varying length underlying arrays. If you want to just store a rectangular matrix of information that is a constant size then doing the one dimensional array trick is often a better idea so it is all in one contiguous block of memory. However, multidimensional arrays proper can still be useful or necessary, so I'd recommend going over the syntax in that link or other references and ask specific questions from there if need be.
Posts
I thought multidimensional arrays were "arrays of arrays?"
A[0][0] + (n * i + j) * size(A).
Google; 5 minutes.
char* A1 = A[0] doesn't copy the array at all. Also, what is your declaration for A here? What you're posting is fairly jumbled.
Lets say that you do the following:
Then B is just a pointer to the location of memory where the first element of A is stored. So if you dereferenced B directly you would get 10, but B is not a copy of A. If you changed the data in A, what you would get out of dereferencing B would be different as well, and the same would be true if you changed the array by accessing it through B. Because they both would point to the same place where the underlying data resides.
Here's a good place to go over some of the array basics: http://www.cplusplus.com/doc/tutorial/arrays/
I need to know where you found that; I looked through at least a dozen forums and articles and got zip.
Multidimensional arrays in C++ are arrays of arrays, and it is possible to make them such that they have varying length underlying arrays. If you want to just store a rectangular matrix of information that is a constant size then doing the one dimensional array trick is often a better idea so it is all in one contiguous block of memory. However, multidimensional arrays proper can still be useful or necessary, so I'd recommend going over the syntax in that link or other references and ask specific questions from there if need be.
Here.