Hi,
In java, you can extract the height and width of an array. In C++, how can u
do the same?
In java, I do this
int width = img[0].length;
int height = img.length;

Type of img is int[][]
Thanks in advance
Jack

Re: C equivlence of int[][] x.length by Jack

Jack
Tue Jun 06 04:44:43 CDT 2006

the plusplus sign is dropped out by the news reader. Don't know how to fix
it.
Thanks
Jack

"Jack" <jl@knight.com> ¼¶¼g©ó¶l¥ó·s»D:u%23irRvTiGHA.1208@TK2MSFTNGP02.phx.gbl...
> Hi,
> In java, you can extract the height and width of an array. In C, how can u
> do the same?
> In java, I do this
> int width = img[0].length;
> int height = img.length;
>
> Type of img is int[][]
> Thanks in advance
> Jack
>
>



Re: C++ equivlence of int[][] x.length by Mark

Mark
Tue Jun 06 04:48:39 CDT 2006

You must use vector<int>'s

C++ has no concept of a native managed array.

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."
"Jack" <jl@knight.com> wrote in message
news:u%23irRvTiGHA.1208@TK2MSFTNGP02.phx.gbl...
> Hi,
> In java, you can extract the height and width of an array. In C++, how can
> u do the same?
> In java, I do this
> int width = img[0].length;
> int height = img.length;
>
> Type of img is int[][]
> Thanks in advance
> Jack
>
>



Re: C++ equivlence of int[][] x.length by Alex

Alex
Tue Jun 06 06:22:40 CDT 2006

Jack wrote:
> In java, you can extract the height and width of an
> array. In C++, how can u do the same?
> In java, I do this
> int width = img[0].length;
> int height = img.length;
>
> Type of img is int[][]

In C++ you can do this only if dimensions of an array were
known at compile time:

int a[2][3];

int width = sizeof(a[0])/sizeof(a[0][0]);
int height = sizeof(a)/sizeof(a[0]);

If you allocate array dynamically, there is no way to know
its actual dimentions. That's why you should use std::vector
as Mark Randall suggests. std::vector has member function
size().



Re: C++ equivlence of int[][] x.length by Tosha

Tosha
Tue Jun 06 07:55:31 CDT 2006

"Alex Blekhman" <xfkt@oohay.moc> wrote in message
news:OqnhGuViGHA.4144@TK2MSFTNGP02.phx.gbl...
> Jack wrote:
>> In java, you can extract the height and width of an
>> array. In C++, how can u do the same?
>> In java, I do this
>> int width = img[0].length;
>> int height = img.length;
>>
>> Type of img is int[][]
>
> In C++ you can do this only if dimensions of an array were known at
> compile time:
>
> int a[2][3];
>
> int width = sizeof(a[0])/sizeof(a[0][0]);
> int height = sizeof(a)/sizeof(a[0]);
>
> If you allocate array dynamically, there is no way to know its actual
> dimentions. That's why you should use std::vector as Mark Randall
> suggests. std::vector has member function size().

http://msdn2.microsoft.com/en-us/library/z2s077bc(VS.80).aspx

#include <cstdlib>
#include <cstdio>
#include <malloc.h> // Req. by _msize

---
void PrintMatNoSizeGiven(int **mat)
{
for (int i = 0; i<_msize(mat)/4 ; i++)
{
for (int j=0 ; j<_msize(mat[i])/4 ; j++)
printf("%3d",mat[i][j]);
printf("\n");
}
}

int main(int argc, char **argv)
{
const int size = 10;

int **mat = (int**)malloc(size*sizeof(void*));
for (int i=0 ; i<size ; i++)
{
mat[i] = (int*)malloc(size*sizeof(int));
for (int k=0 ; k<size ; k++) mat[i][k] = k+1+i;
}
PrintMatNoSizeGiven(mat);
return 0;
}
---






Re: C++ equivlence of int[][] x.length by Carl

Carl
Tue Jun 06 09:02:52 CDT 2006

Tosha wrote:
> "Alex Blekhman" <xfkt@oohay.moc> wrote in message
> news:OqnhGuViGHA.4144@TK2MSFTNGP02.phx.gbl...
>> Jack wrote:
>>> In java, you can extract the height and width of an
>>> array. In C++, how can u do the same?
>>> In java, I do this
>>> int width = img[0].length;
>>> int height = img.length;
>>>
>>> Type of img is int[][]
>>
>> In C++ you can do this only if dimensions of an array were known at
>> compile time:
>>
>> int a[2][3];
>>
>> int width = sizeof(a[0])/sizeof(a[0][0]);
>> int height = sizeof(a)/sizeof(a[0]);
>>
>> If you allocate array dynamically, there is no way to know its actual
>> dimentions. That's why you should use std::vector as Mark Randall
>> suggests. std::vector has member function size().
>
> http://msdn2.microsoft.com/en-us/library/z2s077bc(VS.80).aspx
>
> #include <cstdlib>
> #include <cstdio>
> #include <malloc.h> // Req. by _msize
>
> ---
> void PrintMatNoSizeGiven(int **mat)
> {
> for (int i = 0; i<_msize(mat)/4 ; i++)

... which isn't portable, and only works if the memory was allocated by
malloc (or new[], which calls malloc in the VC++ implementation).

in general, this won't work, and it can't tell you the individual dimensions
of a multidimensional "rectangular array" (which you've shown here is the C
equivalent of a "jagged array" that just happens to be rectangular.

Still, this technique will work in some situations.

-cd