|
From: Ramana K. <ra...@my...> - 2002-09-05 13:30:53
|
you must use a for loop because each value of x in arr[x] must be assigned a
new int[ncolumns], not just one value.
the sizeof operator, i do not know how it works, but it sometimes behaves
unexpectedly especially with multidimensional arrays, and it responds
differently in different scopes. someone else can explain this better.
why must you have int **arr?
what about int* arr[nrows][ncolumns]? this is safer, even if only because it
lets you think more logically so you don't accidently read past the end of
the array.
you would then have a nested for loop to give each item in the
multidimensional array a new int.
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of senem beken
Sent: Thursday, 5 September 2002 7:50 PM
To: dev...@li...
Subject: [Dev-C++] q3??
include <iostream>
#include <stdlib.h>
int main()
{
int nrows,ncolumns;
nrows=ncolumns=5;
int **arr=new int*[nrows];
for(int i=0;i<ncolumns;i++) //why do i have to use a for loop
arr[i]=new int[ncolumns]; //to create a multiple subs array
// dynamically??
//why can't i use a code like:
; //int **arr=new int*[nrows];
// *arr=new int[ncolumns]; ???
for(int i=0;i<nrows;i++)
for(int j=0;j<ncolumns;j++)
arr[i][j]=0;
for(int i=0;i<nrows;i++)
for(int j=0;j<ncolumns;j++)
cout<<arr[i][j]<<" ";
cout<<sizeof(arr)/sizeof(int)<<endl; //and why does this part return 1
//instead of 25??
cout<<nrows<<" "<<ncolumns<<endl;
system("PAUSE");
return 0;
}
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
|