Why we need to give some brackets to make it work? (&arr) rather than &arr (edited) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why we need to give some brackets to make it work? (&arr) rather than &arr (edited)

template<typename T, size_t maxR, size_t maxC> void printArray(T (&arr)[maxR][maxC]) { for(int r = 0; r < maxR; r++) { for(int c = 0; c < maxC; c++) { cout << arr[r][c] << " "; } cout << endl; } }

30th Nov 2018, 1:55 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
15 Answers
+ 3
as we know an array type is passed into a function as a pointer to the first element of the array, you can use array notation (int[]) or pointer notation (int*) in the function declaration, the compiler always treats it as pointer (int*). for example, the following declarations are equivalent: void print(int array[], int size); void print(int *array, int size); void print(int array[5], int size); so "int array[5]" in size_ptr function will be treated as "int*" by the compiler, as follow, the size is lost; so the expression inside size_ptr function "sizeof(arr)/sizeof(int)" becomes sizeof(int *)/sizeof(int) which results 1 for 32 bit machine (size of int and int * is 4) while on 64 bit machine it's 2 ( size of int* is 8 and size of int is 4).
30th Nov 2018, 9:56 PM
MO ELomari
+ 6
Ketan Lalcheta yes, that was i'm trying to ask, btw thanks for your code. It give me little bit education.. :)
30th Nov 2018, 3:36 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 6
Thanks for your help Mohamed ELomari, i think it solved now..
30th Nov 2018, 10:11 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 4
Mustafa K. Yes i know, but thats not the answer was i looking for. I need an explaination why the code doesn't work with &arr reference or lets say, i want to know how the code works...
30th Nov 2018, 3:06 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 4
Mohamed ELomari, so you mean is (&arr) will be treated as reference and arr will be treated as pointer but both still passed as reference? other than that, the difference is on parsing an address(&) and the other one is parsing a pointer(*)?
30th Nov 2018, 8:25 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 4
Mohamed ELomari Thanks for your code, i've already check that out but there is i do not understood. Why void size_ptr gived an output 2?
30th Nov 2018, 8:29 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
Zarthan , I m curious to know reason why you need & when below also works? template<typename T, size_t maxR, size_t maxC> void printArray(T (&arr)[maxR][maxC]) if you want to pass it as reference, array are already by default passed as reference...
30th Nov 2018, 3:07 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
Ketan Lalcheta you mean passing arr with &arr is same?
30th Nov 2018, 3:08 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 3
Ketan Lalcheta, because i want to know why the code works with (&arr) not &arr. I thought (&arr) with &arr is same.
30th Nov 2018, 3:11 PM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 2
the &arr and (&arr) are totally two things different: int & arr[10]; // this is parsed as an array of ten references and that's not a legal type in C++, just as pointers to references and references to references are prohibited ( C++ Standard §8.3.2/4 ). int (&arr)[10]; // this is parsed as a reference to an array of ten
30th Nov 2018, 4:57 PM
MO ELomari
+ 1
As far as I know, it's syntax. To me, your question is like, why displaying something requires cout and not print (Please correct me if I am wrong).
30th Nov 2018, 2:49 PM
Mustafa K.
Mustafa K. - avatar
+ 1
Zarthan , yes .. array is passed as reference by default. check below sample code : https://code.sololearn.com/cIxGCe6UD7b7/?ref=app
30th Nov 2018, 3:13 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Zarthan , your actual question (why & inside braces) is something which I am also unaware and will know new thing if someone else would answer...
30th Nov 2018, 3:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
30th Nov 2018, 5:53 PM
MO ELomari
+ 1
yes both are still passed as reference and the & here is not the adress-of operator it is the reference operator
30th Nov 2018, 10:01 PM
MO ELomari