Public Group

# Passing array to function- C++

This topic is 3165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Sorry, the definition of CMatrix is :
typedef float CMatrix[16];

I wrote that define incorrectly.

##### Share on other sites
Quote:
 Original post by ehsan2004OK, 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 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 on other sites
Quote:
 Original post by ehsan2004OK, 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 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 formsAnd 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).

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632997
• Total Posts
3009780
• ### Who's Online (See full list)

There are no registered users currently online

×