Thread: dynamic allocation.

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    dynamic allocation.

    How do I declare multi dimensional arrays?

    Code:
    char *ptr;
    ptr = new char[size];
    Worked... but...
    Code:
    char **ptr;
    ptr = new char[3][2];
    Gives an error 'Cannot convert char[2]* to char **'.
    Code:
    char * ptr[];
    ptr = new char [2][3];
    Obviuosly gave an error 'The size of ptr[] is unknown or zero.'

    I tried some other combinations in vain; but they gave errors like 'Lvalue required'.
    I checked many books, but they only give the syntax for single dimensional arrays...
    Please Excuse if this question looks really stupid or if the answer is obvious...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This only works if you know the sizes of your minor dimensions at compile time

    char (*ptr)[2];
    ptr = new char[3][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.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you do nopt make a 2d array dynamically like that in c++. There is no operator new[][].
    You will need to use a couple of loops. It has been covered many times. Search the boards to find code samples.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Boy, now this's quick reply.
    I wanted to declare an array to store the data in a 'raw' file.
    since I don't know the the dimensions of the file forehand, I asked this question...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM