+ 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; } }
15 Respostas
+ 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).
+ 6
Ketan Lalcheta yes, that was i'm trying to ask, btw thanks for your code. It give me little bit education.. :)
+ 6
Thanks for your help Mohamed ELomari, i think it solved now..
+ 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...
+ 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(*)?
+ 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?
+ 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...
+ 3
Ketan Lalcheta you mean passing arr with &arr is same?
+ 3
Ketan Lalcheta, because i want to know why the code works with (&arr) not &arr. I thought (&arr) with &arr is same.
+ 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
+ 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).
+ 1
Zarthan , yes .. array is passed as reference by default. check below sample code :
https://code.sololearn.com/cIxGCe6UD7b7/?ref=app
+ 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...
+ 1
yes both are still passed as reference
and the & here is not the adress-of operator it is the reference operator