![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Location: Philippines
Posts: 21
Rep Power: 0
![]() |
multi dimensional array error
i hav a problem bout arrays... why cant i allocate the variable in this array?
thnx for your help #include<iostream> using namespace std; main() { int r=1,c=1,r2,c2; cout<<"Enter the no. of rows you desire for matrix1:"; cin>>r; cout<<"Enter the no. of coloumns you desire for matrix1:"; cin>>c; cout<<"Enter the no. of rows you desire for matrix2:"; cin>>r2; cout<<"Enter the no. of coloumns you desire for matrix2:"; cin>>c2; if(r2>c) cerr<<"invalid row input"; int **matri1[]=new int[r][c];// source of error<----- this is it for(int i=0;i<r;i++) { for(int j=0;j<c;j++) cin>>matri1[i][j]; } for(int k=0;k<r;k++) { for(int j=0;j<c;j++) { cout<<matri1[k][j]; } cout<<endl; } } |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
int **matri1[]=new int[r][c];// source of error<----- this is it
new returns a pointer to whatever you just made. you just made a 2-d array of ints. you're trying to assign a 2-d array of ints to a pointer to a pointer to a single array of ints or some other weird crap. your types don't jive... apple = 3; purple = 3.14159; this shit doesn't work...the type of the thing on the left has to be the same as the type of the thing on the right. actually i'm not even sure what you did, but this is weird! if you have X = Y; then X and Y have to be the same kind of thing.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
First off, you're making a pointer to a pointer to an array of integers. From a simplistic view (though not entirely accurate, grumpy or Narue can and have explained much better if you want the nitty-gritty) this could be simplified as either int*** or int[][][]. I'm guessing that's not what you want.
For the sake of I-should-be-in-bed-right-now, I'll just give a quick sample which should work... but if it doesn't, well, sorry int **matri1 = new int*[r]; for(int i = 0; i < r; i++) matri1[i] = new int[c];
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 10:05 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 3:12 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 6:36 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 3:36 AM |