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

Discussion in 'C++' started by =?utf-8?B?5YiY5piK?=, Jul 26, 2007.

  1. 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.
     
    =?utf-8?B?5YiY5piK?=, Jul 26, 2007
    #1
    1. Advertising

  2. =?utf-8?B?5YiY5piK?=

    Guest

    On Jul 26, 10:13 am, <> 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
     
    , Jul 26, 2007
    #2
    1. Advertising

  3. =?utf-8?B?5YiY5piK?=

    api Guest

    On Jul 26, 9:13 am, <> 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
     
    api, Jul 26, 2007
    #3
  4. =?utf-8?B?5YiY5piK?=

    Ian Collins Guest

    Re: About the retrogression problem when passing a array variableinto a function which takes a pointer as its argument.

    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.
     
    Ian Collins, Jul 26, 2007
    #4
  5. =?utf-8?B?5YiY5piK?=

    Bo Persson Guest

    ?? 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
     
    Bo Persson, Jul 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    674
    sushant
    Jan 20, 2005
  2. jimjim
    Replies:
    16
    Views:
    845
    Jordan Abel
    Mar 28, 2006
  3. Vijai Kalyan
    Replies:
    4
    Views:
    715
    Vijai Kalyan
    Nov 8, 2005
  4. Vincent RICHOMME

    How to be sure my array only takes pointer

    Vincent RICHOMME, Aug 25, 2006, in forum: C++
    Replies:
    7
    Views:
    293
    Jarmo Muukka
    Aug 26, 2006
  5. Replies:
    1
    Views:
    293
Loading...

Share This Page