Sign in to follow this  
ehsan2004

Passing array to function- C++

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
>> 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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this