|
From: Per W. <pw...@ia...> - 2002-09-05 14:02:56
|
Nothing strange with the sizeof operator since arr is a pointer (a pointer to a pointer is also a pointer :-), sizeof arr is of course the size of a pointer. For many machines - do never use this as a fact unless you write hw drivers or something - the size of a pointer is the same as the size of an integer, so sizeof(arr)/sizeof(int) should give the value 1. When you define an array as int x[10], then x is NOT a pointer, even if it may be used instead of a pointer on the right-hand side of assignments. This means that sizeof(x)/sizeof(*x) = sizeof(x)/sizeof(int) = 10. About the loop for allocation of the second index of the array. It should be obvious why it is needed if you look at the following picture. The first allocation allocates a vector (one-dimensional array) of pointers to integers. Then each of these pointers must be initialized to a vector of integers, i.e. a loop is needed. |------------ |------------ |------------ |------------ |------------ |------------ Cheers /Per Westermark EMA System email: pw...@ia... Per Westermark web: http://iapetus.neab.net Vargvaegen 174B phone: +46 8 410 17 198 S-906 42 Umeaa Sweden On Thu, 5 Sep 2002, Ramana Kumar wrote: > 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 > > > > ------------------------------------------------------- > This sf.net email is sponsored by: OSDN - Tired of that same old > cell phone? Get a new here for FREE! > https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |