1

I have a code that is written in java and I want to convert it into c++, I don't find a way to calculate the length of the array in the c++

Java Code:

public void printPerm(int[][] p){
    for(int i=0;i<p.length;i++){
        for(int j=0;j<p[i].length;j++)
            System.out.print(p[i][j]);
        System.out.println();
    }
}

Converted c++ code

void printPerm(int **p)
{
 for (int i = 0;i < sizeof(p) / sizeof(p[0]) ;i++)
 {
    for (int j = 0;j < (p[i].length);j++)
    {
        cout << p[i][j]; 
    }
    cout << endl;
 }
}

I need to convert "p[i].length" to corresponding one in C++, where i will loop through the number of items in array p and j will loop through the length of each item in the array p[i]

deviantfan
  • 11,268
  • 3
  • 32
  • 49
redRose
  • 83
  • 1
  • 3
  • 10
  • if you need it just for this code then make a separate function in which you count length otherwise always use container classes if you happen to do it more often. – Naseer Apr 20 '14 at 09:07

4 Answers4

6

There's no built-in way to find the length of a plain array in C++. (sizeof only works when you have the original array, not when you have a pointer to it.)

There are three solutions:

  • Keep track of the length yourself
  • Use a standard container like std::array or std::vector
  • Make the array one element longer than it needs to be, and add a sentinel value to the end.
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
2

That's not going to work. p is a pointer, so sizeof(p) will be the size of the pointer -- probably 8. p[0] is also a pointer, so its size will be the same.

C++ raw arrays don't store their size like Java arrays do; you need to keep track of that separately. A better strategy might be to use container classes (such as std::vector) instead of raw arrays.

user3553031
  • 5,990
  • 1
  • 20
  • 40
1

Unlike Java c and C++ does not pass along the size of the array.

You have to do that yourself.

Alternatively there is a solution for C++.

It is called std::vector and will behave in a similar fashion to Java

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

If the argument of printPerm is indeed a two-dimensional array then it can not be implicitly converted to type int **. If the argument has indeed type int ** then you need declare additional two parameters that will specify the number of subarrays and the number of elements in each subarray. Pointers in C++ do not encapsulate information about how many elements they refer to.

Let assume that argument was defined the following way

int m = 10;
int n = 20;

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

for ( int i = 0; i < m; i++ ) p[i] = new int [n];

In this case the function should be defined as

void printPerm( const int **p, int m, int n )
{
   for ( int i = 0; i < m; i++ )
   {
      for ( int j = 0; j < n; j++ )
      {
         std::cout << p[i][j]; 
      }
      std::cout << std::endl;
   }
}

and called as

printPerm( p, m, n );

If the argument was defined the following way

const int M = 10;
const int N = 20;
int p[M][N];

then the function should be defined the following way

void printPerm( const int ( *p )[N], int m )
{
   for ( int i = 0; i < m; i++ )
   {
      for ( int j = 0; j < N; j++ )
      {
         std::cout << p[i][j]; 
      }
      std::cout << std::endl;
   }
}

and called as

printPerm( p, M );

Also for the second case there is a possibility to pass an array by reference. The function definition will look as

const int M = 10;
const int N = 20;

void printPerm( const int ( &p )[M][N] )
{
   for ( int i = 0; i < M; i++ )
   {
      for ( int j = 0; j < N; j++ )
      {
         std::cout << p[i][j]; 
      }
      std::cout << std::endl;
   }
}

And can be called as

int p[M][N];
printPerm( p );

EDIT: I have updated some typos.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335