Hi there??
How do you convert the following operation from java to C++?
ncols = (int) floor[0].length;
where ncols = number of columns and floor is an array declared as: char floor[20[20];
thankyou.
Atif.
Printable View
Hi there??
How do you convert the following operation from java to C++?
ncols = (int) floor[0].length;
where ncols = number of columns and floor is an array declared as: char floor[20[20];
thankyou.
Atif.
Well the operation will work in C++ except for the .length
Since characters can only hold one letter floor[0]'s length is 1 unless it is empty.
If you want the length of a whole array like for example:
int ncols; //Create
char array[20]; // Variables
strcpy(array, "afewletters"); // Cope a string into the array
ncols = strlen(array); // return length of string in the array
I think this'll work but since i've got no compiler here in school i dont know....
And a question. Why do you cast floor[0].length to int, isnt it an int already?
Hi ?
what i meant was i have a 2d array called floor[20][20] of type char. and i want to cast it, like this: int ncols = (int) floor[0].length;
this is in java how do i convert it to C++??
thanks
Atif.
C++ doesn't have the length() mehtod that Java has so you have to figure it out yourself. As mentioned above, if it's a string you can use strlen() or another common way is:
This only works on static arrays however, trying this on dynamic arrays will result in the size of the pointer divided by the type :(.Code:int length = sizeof( array ) / sizeof( array_type );
// so in your case
int length = sizeof( floor[0] ) / sizeof( char );
If you use Java's ability to have non-rectangular matrices then this gets more difficult.
An easy solution would be to create a data type (struct or class) that has length variables you can easily access and set. Then you could use the same style of code.