using new to create a two-dimensional array

This is a discussion on using new to create a two-dimensional array within the C++ Programming forums, part of the General Programming Boards category; Can you use new to declare a two-dimensional array from a pointer? So far, I have this: int main() { ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    using new to create a two-dimensional array

    Can you use new to declare a two-dimensional array from a pointer? So far, I have this:

    int main()
    {
    float *a;
    a = new float[1][1];
    return 0;
    }


    I get the following error in the line with the new statement:
    '=' : cannot convert from 'float (*)[1]' to 'float *'

    Can you not declare a two-dimensional array from a pointer?

    Thanks

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    I doubt it works the way you do it. You could make a "pointer to pointers" which is kind of 2D.
    Another solution is to make a 1D array which serves as a 2D:

    int* Pointer;
    Pointer=new int[x*y];


    Remember to deallocate the memory before you quit the program:

    delete[] Pointer;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: reply

    Originally posted by Magos
    I doubt it works the way you do it. You could make a "pointer to pointers" which is kind of 2D.
    Another solution is to make a 1D array which serves as a 2D:

    int* Pointer;
    Pointer=new int[x*y];


    Remember to deallocate the memory before you quit the program:

    delete[] Pointer;
    Wouldn't that be interpreted as a 1-D array of size (x*y), i.e., x= 2, y = 3, an array Pointer = new int[6]; ?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes

    Quote:
    Another solution is to make a 1D array which serves as a 2D

    Access it through Pointer[x+y*xsize]. Element x=2, y=4 in a 5x7 array is element 2+4*5=22 in this "simplified" array. You use a 1D array but handle it somewhat like a 2D array...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read zens post here for an example on how to do this properly.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  3. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 12:09 PM

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