count of pointer to array question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jackie

    count of pointer to array question

    Does anyone know how to get a count for a pointer to an array

    for example

    double** someFnct () {

    cout <<"Enter num of row: " <<endl;
    cin >> n;

    cout <<"Enter num of column: " <<endl;
    cin >> m;

    double** matrixX = new double[n];
    for (int i = 0; i<numOfRows; i++) [
    matrixX[i] = new double(m);
    }
    return matrixX;
    }

    double** tmpMtrx = someFunct();

    int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
    int column = // don't even know where to begin with this one


    Any insight would be greatly appreciated. Thanks
  • Nils O. Selåsdal

    #2
    Re: count of pointer to array question

    On Sat, 16 Oct 2004 18:08:54 -0700, Jackie wrote:
    [color=blue]
    > Does anyone know how to get a count for a pointer to an array
    >[/color]
    ....[color=blue]
    > int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
    > int column = // don't even know where to begin with this one
    >
    >
    > Any insight would be greatly appreciated. Thanks[/color]
    basically you have to record the no. of cols/rows.
    double** someFnct (int &rows, int &cols);

    Comment

    • Victor Bazarov

      #3
      Re: count of pointer to array question

      "Jackie" <darkusang@yaho o.com> wrote...[color=blue]
      > Does anyone know how to get a count for a pointer to an array[/color]

      I don't think you describe it well, but IIUIC, you are trying to
      get the size of a dynamically allocated array from the pointer.
      That is not possible in C++.
      [color=blue]
      >
      > for example
      >
      > double** someFnct () {
      >
      > cout <<"Enter num of row: " <<endl;
      > cin >> n;
      >
      > cout <<"Enter num of column: " <<endl;
      > cin >> m;
      >
      > double** matrixX = new double[n];[/color]

      I think you want

      double** matrixX = new double*[n];
      [color=blue]
      > for (int i = 0; i<numOfRows; i++) [[/color]

      There is no 'numOfRows'. I believe you called it 'n'. So this
      should be

      for (int i = 0; i < n; ++i) {
      [color=blue]
      > matrixX[i] = new double(m);[/color]

      And here you probably want

      matrixX[i] = new double[m];
      [color=blue]
      > }
      > return matrixX;
      > }
      >
      > double** tmpMtrx = someFunct();
      >
      > int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
      > int column = // don't even know where to begin with this one[/color]

      If you want to know what sizes the array was allocated with, pass
      addresses of two integers into your 'someFunct' and let it tell
      you what sizes it used:

      double ** someFunc(int* nRows, int* nCols) {
      ...
      if (nRows) *nRows = n;
      if (nCols) *nCols = m;
      return matrixX;
      }

      ...

      int rows, columns;
      double** tmpMtrx = someFunct(&rows , &columns);

      Otherwise, ask your questions outside and let someFunct allocate
      the memory based on the numbers you pass in.

      Victor


      Comment

      • John Harrison

        #4
        Re: count of pointer to array question


        "Jackie" <darkusang@yaho o.com> wrote in message
        news:e78463d9.0 410161708.11b3b 6b9@posting.goo gle.com...[color=blue]
        > Does anyone know how to get a count for a pointer to an array
        >[/color]

        Impossible, use a vector instead. Look up vectors in your favourite C++
        book.

        john


        Comment

        • Ioannis Vranos

          #5
          Re: count of pointer to array question

          Jackie wrote:[color=blue]
          > Does anyone know how to get a count for a pointer to an array
          >
          > for example
          >
          > double** someFnct () {
          >
          > cout <<"Enter num of row: " <<endl;
          > cin >> n;
          >
          > cout <<"Enter num of column: " <<endl;
          > cin >> m;
          >
          > double** matrixX = new double[n];
          > for (int i = 0; i<numOfRows; i++) [
          > matrixX[i] = new double(m);
          > }
          > return matrixX;
          > }
          >
          > double** tmpMtrx = someFunct();
          >
          > int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
          > int column = // don't even know where to begin with this one
          >
          >
          > Any insight would be greatly appreciated. Thanks[/color]


          You can't, you will use variables n and m for that.


          A better way is to do:


          #include <iostream>
          #include <vector>

          int main()
          {
          using namespace std;

          unsigned m ,n;

          cout <<"Enter num of row: " <<endl;
          cin >> n;

          cout <<"Enter num of column: " <<endl;
          cin >> m;

          vector<vector<d ouble> >matrixX(n, vector<double>( m));
          }



          In this matrixX.size() returns the number of rows, and matrixX[i].size()
          returns the number of columns.



          --
          Ioannis Vranos


          Comment

          • Gianni Mariani

            #6
            Re: count of pointer to array question

            Jackie wrote:[color=blue]
            > Does anyone know how to get a count for a pointer to an array
            >
            > for example
            >
            > double** someFnct () {
            >
            > cout <<"Enter num of row: " <<endl;
            > cin >> n;
            >
            > cout <<"Enter num of column: " <<endl;
            > cin >> m;
            >
            > double** matrixX = new double[n];
            > for (int i = 0; i<numOfRows; i++) [
            > matrixX[i] = new double(m);
            > }
            > return matrixX;
            > }
            >
            > double** tmpMtrx = someFunct();
            >
            > int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
            > int column = // don't even know where to begin with this one
            >
            >
            > Any insight would be greatly appreciated. Thanks[/color]

            A suggestion: use a matrix class.



            There are plenty of open source matrix libraries.

            G

            Comment

            • Jackie

              #7
              Re: count of pointer to array question

              Thanks everyone,

              I appreciate the suggestions and comments, it was enlightening and
              helpful. I apologize for the careless code post. I went with vectors
              instead. Long time Java/SQL programmer, but first week learning C++.
              I'm learning to like C++ more and more.

              Thanks again everyone.

              Jack

              Comment

              Working...