Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About the retrogression problem when passing a array variable into a function which takes a pointer as its argument.

Reply
Thread Tools

About the retrogression problem when passing a array variable into a function which takes a pointer as its argument.

 
 
=?utf-8?B?5YiY5piK?=
Guest
Posts: n/a
 
      07-26-2007
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.

 
Reply With Quote
 
 
 
 
tony_in_da_uk@yahoo.co.uk
Guest
Posts: n/a
 
      07-26-2007
On Jul 26, 10:13 am, <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
api
Guest
Posts: n/a
 
      07-26-2007
On Jul 26, 9:13 am, <(E-Mail Removed)> 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.


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

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-26-2007
api wrote:
>
> 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.

--
Ian Collins.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      07-26-2007
?? 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM



Advertisments