Thread: 2-D dynamic array

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    32

    2-D dynamic array

    Hi guys, I am trying to create a 2d dynamic array with the code:

    Code:
    	char **Split_Text;
    	Split_Text = new *char[Vigenerek.value];	// new dynamic array
    	for(i=0;i<Vigenerek.value;++i) Split_Text[i] = new char[ArrayLen];
    but digital mars keep giving me "need explicit cast to convert from int ** to char **. I don't really get what is it saying since i don't have a single int pointers. can somebody please help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you put those 3 lines, and a minimal amount of support lines necessary to create a complete compilable program (it doesn't have to do anything), and post that.

    Certainly looks pretty odd to me though, as you say, no obvious sign of an int.
    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
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    I don't have much experience with dynamic arrays but can't you just say
    Code:
    char *x[WIDTH];//array of pointers to chars (one for each column)
    for(i=0;i<WIDTH;++i) 
        x[i] = new char[HEIGHT];

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char **Split_Text;
    Split_Text = new *char[Vigenerek.value];	// new dynamic array
    for(i=0;i<Vigenerek.value;++i) Split_Text[i] = new char[ArrayLen];
    That * is in the wrong place, it needs to be:
    Code:
    char **Split_Text;
    Split_Text = new char*[Vigenerek.value];	// new dynamic array
    for(i=0;i<Vigenerek.value;++i) Split_Text[i] = new char[ArrayLen];
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Good catch hk_mp5kpdw.
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be there's a reason why you need an array of char arrays, but to get things done a much simpler way is:
    Code:
    #include <vector>
    #include <string>
    
    int main()
    {
        std::vector<std::string> Split_Text; //voil&#224;
    
        // or to set memory aside out-front:
        std::vector<std::string> Split_Text(Vigenerek.count, std::string(ArrayLen));
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    32
    Thanks guys, thats very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM