Thread: 2d array with new[]

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    2d array with new[]

    I use a 2d array and I declare it like this:

    Code:
    double * table = new double[2][num];
    the integer "num" is entered by the user. Dev-C++ gives errors:

    functions.h:47: cannot convert `double (*)[((num - 1) + 1)]' to `double*' in
    initialization
    What is wrong?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    Well as far as I know, you can only make 1D arrays dynamically.

    Since you know the size of one bit of it, why not just create an array of pointers like this:
    Code:
    double *table[2];
    Then you just go through each element of the array and create a new double of length 'num':
    Code:
    for (int x = 0; x < 2; x++)
        table[x] = new double[num];

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I suppose you could do something like this:
    Code:
    const int table_size = 2;
    double* table[table_size];
    
    for (int i = 0; i < table_size; ++i) {
    	table[i] = new double[num];
    }
    
    // ...
    
    for (int i = 0; i < table_size; ++i) {
    	delete[] table[i];
    }
    Have you considered using vectors?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > double * table = new double[2][num];
    You can only do multiple dimensions like this if all the minor dimentions are compile-time constants.

    Eg.
    double (*table)[2] = new double[num][2];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Thanks for the replies!

    @laserlight:
    I don't know how to use vectors yet
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are learning C++, many would suggest learning vectors before C style arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM