Connecting Tech Pros Worldwide Help | Site Map

alloc 2d array with new

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:58 AM
aurgathor
Guest
 
Posts: n/a
Default alloc 2d array with new

I'm trying to allocate a 2d array in runtime
that can subsequently be accessed like
arr[x][y], to no avail. Is there any to
do that in C++? If yes, how?

TIA

This is what I got thus far:

#include <iostream>

struct sq_T {
short X;
short Y;
int ID;
};

void maker ( int x, int y ) {
sq_T *display = new sq_T[x*y];

// test code
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
display[(j * x) + i].ID = i + (j*x);

for (int j = 0; j < y; j++) {
for (int i = 0; i < x; i++) {
cout << display[(j * x) + i].ID << " ";
}
cout << endl;
}
}


void main () {
maker(5,7);
}



  #2  
Old July 23rd, 2005, 01:58 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: alloc 2d array with new

"aurgathor" <spam-me-if@you.com> wrote...[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?
> [..][/color]

This is covered in the FAQ (http://www.parashift.com/c++-faq-lite/)


  #3  
Old July 23rd, 2005, 01:58 AM
Michael Mair
Guest
 
Posts: n/a
Default Re: alloc 2d array with new

aurgathor wrote:[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?[/color]

Start at
http://www.parashift.com/c++-faq-lit...html#faq-16.16
and read also the next couple of FAQs.

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
  #4  
Old July 23rd, 2005, 01:58 AM
aurgathor
Guest
 
Posts: n/a
Default Re: alloc 2d array with new


"Michael Mair" <Michael.Mair@invalid.invalid> wrote in message
news:39he66F62f81qU1@individual.net...[color=blue]
>
> Start at
> http://www.parashift.com/c++-faq-lit...html#faq-16.16
> and read also the next couple of FAQs.
>[/color]

Thanks, I guess it would've taken a little time to figure all that out.


  #5  
Old July 23rd, 2005, 01:59 AM
Axter
Guest
 
Posts: n/a
Default Re: alloc 2d array with new


aurgathor wrote:[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?
>
> TIA
>
> This is what I got thus far:
>
> #include <iostream>
>
> struct sq_T {
> short X;
> short Y;
> int ID;
> };
>
> void maker ( int x, int y ) {
> sq_T *display = new sq_T[x*y];
>
> // test code
> for (int j = 0; j < y; j++)
> for (int i = 0; i < x; i++)
> display[(j * x) + i].ID = i + (j*x);
>
> for (int j = 0; j < y; j++) {
> for (int i = 0; i < x; i++) {
> cout << display[(j * x) + i].ID << " ";
> }
> cout << endl;
> }
> }
>
>
> void main () {
> maker(5,7);
> }[/color]


Check out the following link:
http://www.tek-tips.com/faqs.cfm?fid=5575

It has several more efficient methods for creating 2D array.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.