Thread: dynamically allocating 2D arrays

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    8

    dynamically allocating 2D arrays

    ok, so my programming partner and I just found out that you can't dynamically allocate a 2d array in C++. like this

    Code:
    char* charPtr;
    
    charPtr = new char[someVariable][someVariable2];
    C++ says no. Does anyone know a way around this?

    we already have a bunch of code based around it and we need a fix.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    sorry I didn't catch that in the FAQ. it has been months since I have read that thing. THANKS.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    another quick question on the 2D array problem. how would you dereference something in that?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how would you dereference something in that?
    Save yourself a migraine and use subscripting just like a two dimensional array:
    Code:
    array[x][y] = someval;
    cout<< array[x][y];
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a number of large 3d arrays in C
    By Surrender in forum C Programming
    Replies: 22
    Last Post: 08-19-2008, 08:36 AM
  2. dynamically allocating 4D arrays
    By Marv in forum C Programming
    Replies: 15
    Last Post: 04-12-2007, 12:34 AM
  3. Dynamically Allocating a 2D Array.
    By LightsOut06 in forum C Programming
    Replies: 17
    Last Post: 10-09-2005, 12:55 AM
  4. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  5. Dynamically allocated arrays
    By axe786 in forum C Programming
    Replies: 6
    Last Post: 12-03-2004, 01:41 AM