Hi guys. I got a question from my homework i dont understand. My professor gave us the answers but I dont understand why they are the answers.
1) Why is a valid? What is arr?
2) Why is c invalid when d is valid?
3) for e, do you always have to pass the numbers in the array?
1 2 3 4 5 6 7 8 9 10 11
typedeffloat ArrType[100][20];
ArrType x;
DoSomething(x);
Are the following valid or invalid for DoSomething?
a. void DoSomething (/* inout */ ArrType arr); //Valid
b. void DoSomething (/* inout */ float arr[100][20]); //Valid
c. void DoSomething (/* inout */ float arr[100][]); //Invalid
d. void DoSomething (/* inout */ float arr[][20]); //Valid
e. void DoSomething (/* inout */ float arr[][]); //Invalid
f. void DoSomething (/* inout */ float arr[][10]); //Invalid
Well, from what I can tell, the code does a type define for an array of floats of dimensions [100][20]. That type-define is then called ArrType. The function DoSomething must be passed something that is consistent with ArrType.
In this case, arr is just some array with those specified dimensions.
So, for each case:
a is valid because DoSomething requires something of type ArrType to be passed to it
b is valid because arr[100][20] is the same as ArrType
c is invalid because you need to know the depth of the internal arrays- it can figure out the external [100] but not the internal [20] on its own.
d is valid under that same premise
e is invalid under that same premise
f is invalid because the depth is not the same as ArrType
a. just like x ,arr is a new variable name for ArrType which is a new allias for float that typedef created ( you may want to read this http://www.cplusplus.com/doc/tutorial/other_data_types/ )
and void DoSomething ( data type variable name ) is a valid declaration of prototype function.
b. void DoSomething (/* inout */ float arr[100][20]); is a normal declaration of prototype function.
c & d & e . i encountered some error about this. when using array as passing to function the second array must have bounds except the first.
f. you cannot resize the array directly just like it void DoSomething (/* inout */ float arr[][10]);