in my code i have declared

typedef signed long long int slong;
const slong limit=4000000;
    const slong limitsqr=2000;

i then created an array

slong* array=new slong[limit];

without any problem.

when i try this

slong* newarray=new slong[limitsqr][limitsqr];

i get the error message:

in function int main()
cannot convert slong(*)[2000] to slong in initialization

can anyone point out where i'm going wrong?

Recommended Answers

All 7 Replies

Try this

slong** newarray = 0;

for( int i = 0; i < limitsqr; i++ )
	newarray[i] = new slong[limit];
const slong limitsqr=2000;
    slong (*newarray)[limitsqr] = new slong[limitsqr][limitsqr] ;
    
    // delete with: delete[] newarray ;

thanks will give this a try(even though i don't really understand it).

i would still like to know why i can create a 4 million 1D array but i can't create
a 4 million 2D array????????

if anyone can explain i would be grateful.:)

i don't really understand it).

i would still like to know why i can create a 4 million 1D array but i can't create
a 4 million 2D array????????

For a dynamically allocated array, eg. new[] T[N] the new expression yields a pointer to the first element of the array. ie. T* ptr_first_element = new T[N] ; slong (*newarray)[limitsqr] = new slong[limitsqr][limitsqr] ; is equivalent to

typedef slong[limitsqr] array_type ;
array_type* newarray = new array_type[limitsqr] ;

We are creating an array with dynamic storage duration where each element is an array; the pointer to the first element is therefore a pointer to an array.

thanks.

Well, it's a couple things:
First, there is no version of the new operator that can create a 2-dimensional array directly. The operator new[] can only be used for 1-dimensional arrays. You must use the operator multiple times to produce a multi-dimensional array.

Second, a 2-dimensional array uses 2-levels of pointer indirection. You declared the pointer with only 1 level of indirection. In essence, you are trying to store a slong* to a slong.

The proper way to do a 2-dimensional array is:

typedef signed long long int slong;
const slong limit=4000000;
const slong limitsqr=2000;

slong **array = new slong*[limitsqr];

for (int i = 0; i < limitsqr; ++i) {
  array[i] = new slong[limitsqr];
}

EDIT:
Oops, a little slow...

For a dynamically allocated array, eg. new[] T[N] the new expression yields a pointer to the first element of the array. ie. T* ptr_first_element = new T[N] ; slong (*newarray)[limitsqr] = new slong[limitsqr][limitsqr] ; is equivalent to

typedef slong[limitsqr] array_type ;
array_type* newarray = new array_type[limitsqr] ;

We are creating an array with dynamic storage duration where each element is an array; the pointer to the first element is therefore a pointer to an array.

:confused: I guess I'm going to have to study this more...

thanks for all the help guys

slong (*newarray)[limitsqr] = new slong[limitsqr][limitsqr] ;

this worked a treat.
gonna have to learn more about pointers by the look of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.