01 Why i am not able to do this

int* px;
px = new int[2][2];

other the px = new int[2*2]; is there any other method

02 Why i am not able do access through pointer dx by dx[1][2] other than by using dx[1*7+2]

static float mat[5][7] =
	{
	        -5, -4,  0,  0,  0,  0,  0,
		 6,  4,  1,  0,  0,  0, 24,
		 1,  2,  0,  1,  0,  0,  6,
		-1,  1,  0,  0,  1,  0,  1,
		 0,  1,  0,  0,  0,  1,  2
	};
	float *dx=&sim_max[0][0];
	cout<<dx[1][0];

03 why i am not able to directly initialize array of new dynamically alloc. mem to

float *dx= new float[5*7]
	(
		-5, -4,  0,  0,  0,  0,  0,
		 6,  4,  1,  0,  0,  0, 24,
		 1,  2,  0,  1,  0,  0,  6,
		-1,  1,  0,  0,  1,  0,  1,
		 0,  1,  0,  0,  0,  1,  2
	);

Recommended Answers

All 4 Replies

Well Dynamic 2D arrays take a little more complexity.This is mainly because when you declare something like this

int *p;
p= new int [2][2];

What you are trying to do is assigning a pointer array to a pointer itself.
THe new int [2][2] converts itself into a pointer array in this case it is something like int * [2] So In order to Dynamically Create a 2D array you will need to do something like this.

int *p,**q;
   q=new int* [2];
   q[0]=new int [2];
   q[1]=new int [2];
   delete []q[0];
   delete [] q[1];
   delete [] q;

So now you have a 2d array whose elements you can access.

I've always found it simpler to allocate a one-dimensional array, and then calculating the row and column manually, it's an easy approach, and it works.

Thanx Sky Diploma
here things got too much complicated

Temp2=T1->dx[i*T1->n+T1->n-1] / T1->dx[i*T1->n+pivot_j];

i guess i have to use the loop for assigning the value inside it..

Yes, Btw, Dont forget deleting each one of the dynamic pointers . Before you delete the pointer to pointer.
And yea, Loops could assign values.
Another solution would be to use vectors.

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.