|
From: Ioannis V. <no...@ho...> - 2002-09-05 11:48:47
|
>From: senem beken <sbe...@ya...>
>To: dev...@li...
>Subject: [Dev-C++] q3??
>Date: Thu, 5 Sep 2002 02:49:38 -0700 (PDT)
>#include <iostream>
>#include <stdlib.h>
Or better: #include <cstdlib>
>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]; ???
You can:
#include <iostream>
#include <vector>
int main() try
{
using std::vector;
int (*p)[50];
// Creates an 100x50 array on the free store
p=new int[100][50];
delete[] p;
// A second way:
typedef int array[50];
// Creates an 100x50 array on the free store
array *r=new array[100];
delete[] r;
// Creates a 100x50 vector. Prefer this
vector<vector<int> >s(100, vector<int>(50));
// s clears itself automatically at the end of its scope.
}
catch(std::bad_alloc)
{
std::cerr<<"I think i saw a catty!\n";
}
>
>
> 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??
Because it is equivallent of the expresion sizeof(int **)/sizeof(int)
which obviously happen to have the same size.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|