-1

This is a java code.

for (int j = 0; j < charArray[i].length; j++) {

           System.out.print(charArray[i][j]);

      }

Very simple question, how I make the java length property but in c++?

jarwin
  • 628
  • 2
  • 10
  • 26
  • 2
    Possible duplicate of [How do I find the length of an array?](http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) – phuclv Feb 20 '16 at 02:55
  • [What is the equivalent of `array.length()` (used in Java) in C++?](https://www.quora.com/What-is-the-equivalent-of-array-length-used-in-Java-in-C++), [How do I use arrays in C++?](http://stackoverflow.com/q/4810664/995714) – phuclv Feb 20 '16 at 02:57
  • 1
    Consider using one of the C++ container classes, such as `std::vector` or `std::array`. – Keith Thompson Feb 20 '16 at 02:57
  • As @KeithThompson for C++ you should try to use class types which give you information like size or length. `std::vector` has a [`size`](http://en.cppreference.com/w/cpp/container/vector/size), `std::string` has [`size` and `length`](http://en.cppreference.com/w/cpp/string/basic_string/size) functions – James Adkison Feb 20 '16 at 03:05
  • @JamesAdkison: You should *usually* try to use class types. Sometimes you do need to work with raw arrays (for example if you're implementing `std::vector`!) – Keith Thompson Feb 20 '16 at 03:06
  • @KeithThompson Yes, you should _try_ to avoid raw arrays when possible. I didn't say _**never**_ use them. Same goes for raw pointers and lots of other things. There are no **rules/laws** just the proper application of knowledge and judgment given a set of requirement and constraints. I didn't suggest or imply otherwise. – James Adkison Feb 20 '16 at 03:08
  • 1
    @JamesAdkison: I believe we're in violent agreement. – Keith Thompson Feb 20 '16 at 03:09
  • @KeithThompson Indeed we are. :) – James Adkison Feb 20 '16 at 03:10

3 Answers3

0

You can make a function call by passing the array pointer and you can check if the pointer is null by

if (charArray != 0)

then you can try this

sizeof(charArray)/sizeof(charArray[0])

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
  • I don't get the 0 in charArray[0]. – jarwin Feb 20 '16 at 02:54
  • 1
    that means size of the whole array divided by size of a single element – phuclv Feb 20 '16 at 02:55
  • 3
    This works only if `charArray` is actually an array. If you've passed it to a function as a parameter, what you've actually passed is probably a pointer, and its size is unavailable. – Keith Thompson Feb 20 '16 at 02:57
  • If it's passed to a function as a parameter, then the function should probably look something like `template void funcName(const char (&charArray)[N})`, so it can keep the size information. ...Although in this case, you could just use `N` for the size, so the point may be moot. – Justin Time - Reinstate Monica Feb 20 '16 at 03:44
0
template <typename T,unsigned S>
inline unsigned arraysize(const T (&v)[S]) { return S; }

The compiler will try to create a function, based on the template, that matches the size of the array. I take no credit for the answer, see the link below, with a lengthier explanation.

http://www.cplusplus.com/forum/general/33669/

It doesn't work for a dynamically allocated array, but it gives you a compiler error. Does anything work for a dynamically allocated array?

Ramon
  • 1,169
  • 11
  • 25
0

Just do this: for(int a=0; charArray[a] != '\0' ; a++); This will store the length of charArray in 'a'

Panda
  • 2,400
  • 3
  • 25
  • 35
  • This only works if it's a null terminated string, and will give you the size of the string, not the array. That is, if the string was stored in an array larger than itself, it will just give the size up to where '\0' is. Also, it will result in segmentation faults if the array is not a null terminated string. – Ramon Feb 20 '16 at 14:00
  • But isn't it like, all the strings are compulsorily terminated with null character. ??? – Panda Feb 20 '16 at 14:05
  • And regarding array is concerned, we only need the length of the string. So I guess, it would return length of string until null character is encountered – Panda Feb 20 '16 at 14:08
  • Well the question does say 'length of char array', so it doesn't have to be a C style string, and it never asks for anything related to 'strings'. However, if that was indeed what OP meant, then yeah that answer would be correct, although there are library functions for it. http://www.cplusplus.com/reference/cstring/strlen/ – Ramon Feb 20 '16 at 14:19