2d array with new[]

This is a discussion on 2d array with new[] within the C++ Programming forums, part of the General Programming Boards category; I use a 2d array and I declare it like this: Code: double * table = new double[2][num]; the integer ...

  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
    21,396
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,317
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21