Question about arrays and pointers, please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • QiongZ
    New Member
    • Nov 2009
    • 7

    Question about arrays and pointers, please help

    I came across this when I was reading a book on Java.

    In the book, there is a note trying to explain what's equivalent in C++ to

    Code:
    double[][] balance = new double[10][6];  // Java
    So my question is what's the difference between

    Code:
    double balance[10][6];
    and
    Code:
    double (*balance)[6] = new double[10][6];
    and

    Code:
    double** balance = new double* [10];

    I am learning c++ without a teacher and have no where to turn to. Arrays and pointers are getting very confusing. Your help is highly appreciated.
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    #2
    double balance[10][6];

    Its an array oy type double with dimension 10X6


    double (*balance)[6] = new double[10][6];

    Its an array of pointers which points to array of type double with dimension 10X6.


    double** balance = new double* [10];

    Balance is a pointer to pointer which points to an array of pointers of size 10.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      double (*balance)[6] = new double[10][6];

      Its an array of pointers which points to array of type double with dimension 10X6.
      double (*balance)[6];

      is not an array of pointers, it is a pointer to an array of size 6. The pointer to the array of size 6 is pointed to an array of size 10 of arrays of size 6.

      An array of pointers would be

      double *balance[6];

      </pedantic>

      Comment

      • puneetsardana88
        New Member
        • Aug 2009
        • 57

        #4
        @Banfa

        You are right

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Read this:

          Comment

          • QiongZ
            New Member
            • Nov 2009
            • 7

            #6
            Thanks everyone and what a great post, weaknessforcats !

            Now I understood

            double (*balance)[6] = new double[10][6];

            but still don't know what

            Code:
            double** balance = new double* [10];
            means in C++?

            In your article, you said

            Code:
            double** balance = new double [10][6];
            would be error.

            Is

            Code:
            new double [10][6];
            equivalent to

            Code:
            new double* [10];
            ?

            Thanks very much!

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Code:
              double** balance = new double* [10];
              balance is a pointer to a pointer to a double. This is actually a very basic array assignment. If you want a dynamically allocated array of some abstract type T then you declare a pointer to T and new an array of T like so

              T *array = new T[<dimension>];

              In this case T is double *, a pointer to double (and <dimension> is 10) giving

              double **array = new double*[10];

              Just the same as allocating a normal int array, setting T to int gives

              int *array = new int[<dimension>];

              new double [10][6];

              is in no way equivalent to

              new double* [10];

              The first allocates a 2 dimensioned array of doubles, or rather an array of arrays of doubles. The second allocates an array of pointers to double. Very very different.

              Comment

              • QiongZ
                New Member
                • Nov 2009
                • 7

                #8
                Thanks a lot Banfa for taking time to explain in detail. Also you explained it in a way very easy to understand.

                Every time I post here, I get excellent answers. I can't thank you guys enough.

                Qiong

                Comment

                Working...