Passing array to function- C++

Started by
6 comments, last by Zahlman 14 years, 2 months ago
Here's part of a source code from an open source project:

inline void CMatrixCopy( const CMatrix LSrcMtx, CMatrix LDstMtx)
{
memcpy(LDstMtx, LSrcMtx, sizeof(CMatrix));
}



And here's the definition of CMatrix:

//I corrected it 
#typedef float CMatrix[16]



And we can use this function like this:

CMatrix srcMtx;
CMatrix dstMtx;
CMatrixCopy( srcMtx, dstMtx );



So How to interpret the arguments of CMatrixCopy function? I know that srcMtx is a pointer to the first element of the array, but what's the meaning of

const CMatrix LSrcMtx = srcMtx; 



which is equal to:

const float LSrcMtx[16] = srcMtx;



? It means that we can initialize an array by another array? I know that the first element of memcpy should be a pointer which points to the first element of LSrcMtx( or srcMtx ). [Edited by - ehsan2004 on February 22, 2010 1:24:32 AM]
Advertisement
Quote:And here's the definition of CMatrix:
#define float CMatrix[16]
What that does it replace all occurrences of the text "float" with the text "CMatrix[16]". So this:
float x;
becomes this:
CMatrix[16] x;
If it was:
typedef float CMatrix[16];
It would probably work. So: what is the real code?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

OK, but it wasn't my question.
My question is " can we initialize an array by the name of another array?
for example:
float AnotherArray[16];
float array[16] = anotherArray;

if yes, what's the meaning of this code?
does it mean that array points to the first element of anotherArray?
Sorry, the definition of CMatrix is :
typedef float CMatrix[16];

I wrote that define incorrectly.
Quote:Original post by ehsan2004
OK, but it wasn't my question.
My question is " can we initialize an array by the name of another array?
for example:
float AnotherArray[16];
float array[16] = anotherArray;
No. Arrays are not first-class types that support assignment like that. They can only initialized in these two forms:
float array[16];float array[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
You can't do this either:
float array1[16];float array2[16];array1 = array2;

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

>> No. Arrays are not first-class types that support assignment like that. They can only initialized in these two forms

And also these :
int Array[10] = {0};int Array[10] = {};


I guess you can argue that Array[10] = {0} is a variant of the form Array[10] = {1,2,3...etc }, but its still not completely the same.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Quote:Original post by ehsan2004
OK, but it wasn't my question.
My question is " can we initialize an array by the name of another array?
for example:
float AnotherArray[16];
float array[16] = anotherArray;

if yes, what's the meaning of this code?
does it mean that array points to the first element of anotherArray?


Not sure what you are trying to do here. This code won't compile. The code above, if written correctly, would basically create two separate arrays.

If you just want one array but accessible from two or more different variables, you need pointers:

float anotherArray[16];
float* array = anotherArray;

array will point to the first element of anotherArray, then you can treat array just like a normal array, and doing these will directly change the content of anotherArray even though anotherArray is not mentioned in the code.

array[2] = 10.0f;

If you want to copy the content from one array to another, then you need memcpy().
Quote:Original post by Concentrate
>> No. Arrays are not first-class types that support assignment like that. They can only initialized in these two forms

And also these :
int Array[10] = {0};int Array[10] = {};


I guess you can argue that Array[10] = {0} is a variant of the form Array[10] = {1,2,3...etc }, but its still not completely the same.


Yes, it is; it's the same syntactical construct. You can also initialize it, for example, like 'int Array[10] = { 4, 2, 7 };'. Basically, you can specify any number of elements that will fit; any unspecified elements are default-constructed (zeroed, for primitive types).

This topic is closed to new replies.

Advertisement