How can i declare 2D array in visual C++ 6.0 as a member of class?
this syntax of mine is giving error.

class sparseadd
{
    int *p;
    int row;
    int col
};

sparseadd::sparseadd(int r, int c)
{
    row = r;
    col = c;
    p = new int[r][c]; // here i am getting errors:icon_question:
}

How would you do this if p was a 1 dimensional array? It doesn't make sense to try and work out how 2D arrays work unless you have good knowledge of 1D arrays.

So show us that you can do this with a 1D array, then I'm sure we can come up with a solution for a 2D array from there.

Variations of this question have been popping up with disturbingly high frequency the last couple weeks. Did you try searching the forums at all?

To dynamically allocate a 2-D array, you need to declare the array pointer as a pointer-to-pointer (i.e. dataType **ppName), not simply a single pointer (i.e. dataType *pName).

for 1 D array i would have done it like this:

class sparseadd
{
int *p;
int row;
//int col
};
sparseadd::sparseadd(int r)
{
row = r;
//col = c;
p = new int[r]; //
}

i have given the same pointer use method on other links as well but i don't know that how the pointer method is different from array of pointers if i use it for 2 D array.

@ fbody
how will i pass that two D array in a function than if i have to decide on runtime the coloumn size of the array.

I saw fbody replied on one of the thread which should answers your question... Can't remember which one... But you need 2 stars in order to refer to the pointer of your 2D array element.

@ fbody
how will i pass that two D array in a function than if i have to decide on runtime the coloumn size of the array.

When you declare an array of size N, what you are actually declaring is a pointer to the first of N contiguous identically-sized blocks of memory. When you access an element of that array, pointer arithmetic on, and dereferencing of, the pointer occur in the background. The array syntax is really just a convenience.

If the size must be determined at run time, the proper way to do it is dynamic allocation and passing the actual pointer, instead of the array.

void someFunc(int **, int, int);        //function prototype

int **anArray = new int *[rows];        //declare the "rows" of the "array"
for (int i = 0; i < rows; ++i) {
  anArray[i] = new int[cols];           //populate the rows
}

someFunc(anArray, rows, cols);          //call the function

//define the function
void someFunc(int **theArray, int rows, int cols) {
  for (int j = 0; j < rows; ++j) {      //define j, traverse dimension 1, the rows
    for (int k = 0; k < cols; ++k) {    //define k, traverse dimension 2, the columns within the rows
      cout << anArray[j][k] << " ";     //output statement
    }                                   //end inner for (index k)
    cout << endl;                       //output statement
  }                                     //end outer for (index j)
}                                       //end function

There are other ways, but this is the way that I have found to be the easiest to implement and understand.

thanks. my issue is resolved now

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.