About the retrogression problem when passing a array variable into a function which takes a pointer

G

Guest

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.
 
T

tony_in_da_uk

typedef int IntArray[100];
int g(IntArray& ia) {
cout << sizeof(ia) << endl;
}

The output is:
400

Can anybody explain what's going on here, a lot of thanks will go to
you.

Isn't it obvious? You're seeing 400 which is 100 * sizeof(int)...?
In other words, the full type information (including array dimension)
is preserved when ia is a reference.

Tony
 
A

api

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.

sizeof a pointer always return 4.
Passing reference as parameters, however, contains the full type
information
 
I

Ian Collins

api said:
sizeof a pointer always return 4.

Only on a system where the size of a pointer is 4...
Passing reference as parameters, however, contains the full type
information
If the compiler knows the type of a variable, parameter or not, it will
know the size.
 
B

Bo Persson

?? 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top