address of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    address of an array

    int myArray[10];

    it is said that array names are address information.
    what i want to ask is
    does myArray mean exactly the same thing with &myArray [0]
    if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

    1 -what is the relation and difference between myArray and &myArray [0]
    2- what is the relation and difference between (*myArray ) and myArray [0]


    thanks in advance...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by stmfc
    int myArray[10];

    it is said that array names are address information.
    what i want to ask is
    does myArray mean exactly the same thing with &myArray [0]
    if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

    1 -what is the relation and difference between myArray and &myArray [0]
    2- what is the relation and difference between (*myArray ) and myArray [0]


    thanks in advance...
    In C (and C++) the name of the array boils down to the address of the first
    element of that array. The sizeof() operator is a bit funny, i.e. it is evaluated
    at compile time: when you do 'sizeof(array)' the compiler knows what you're
    talking about, i.e. the size in bytes taken by the entire array. OTOH when you
    do this: 'sizeof(&array[0])' all the compiler knows is that you've taken the
    address of the first element of that array, which is a simple pointer. So the
    result of both sizeof() examples will most likely be different: the size of your
    array measured in bytes versus the size of a simple pointer.

    kind regards,

    Jos

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by stmfc
      int myArray[10];

      it is said that array names are address information.
      what i want to ask is
      does myArray mean exactly the same thing with &myArray [0]
      if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

      1 -what is the relation and difference between myArray and &myArray [0]
      2- what is the relation and difference between (*myArray ) and myArray [0]


      thanks in advance...
      An array can degrad to a pointer, but it is not one.

      If you pass an array to something that takes a pointer (of the same type as in the array) it will degrade into a pointer. At that point a sizeof will show the size of the pointernot of the array (because it is checked at compile time, not runtime). Example:
      [code=cpp]
      void f(int *array)
      {
      cout << "second element = " << *(array+1) << endl;
      cout << "second element = " << array[1] << endl;
      ++array;
      cout << "second element = " << *array << endl;
      cout << "second element = " << array[0] << endl;
      }

      int main()
      {
      int array[4] = { 1,2,3,4 };
      f(array);
      }
      [/code]

      If you pass an array to something that looks like it is supposed to take an array using the empty brackets ('[]'), beware. This is actually considered a pointer, and a non const one at that. It is for readability only. The sizeof operator works the same way as if it were a pointer. Example:
      [code=cpp]
      void f(int array[])
      {
      cout << "second element = " << *(array+1) << endl;
      cout << "second element = " << array[1] << endl;
      ++array;
      cout << "second element = " << *array << endl;
      cout << "second element = " << array[0] << endl;
      }

      int main()
      {
      int array[4] = { 1,2,3,4 };
      f(array);
      }
      [/code]

      If you pass an array to something that takes a reference to an array, it needs to be the same size.
      [code=cpp]
      void f(int (&array)[4])
      {
      cout << "second element = " << *(array+1) << endl;
      cout << "second element = " << array[1] << endl;
      // ++array; // This is invalid
      }

      int main()
      {
      int array[4] = { 1,2,3,4 };
      f(array);
      }
      [/code]


      Adrian

      Comment

      Working...