How to know the length of the array?

sizeof(ch) 

does not fit because when it goes space, then this ends up

Closed due to the fact that off-topic participants are Vladimir Martyanov , StateItPrimitive , aleksandr barakin , zRrr , insolor 5 Apr '16 at 3:02 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Vladimir Martyanov, aleksandr barakin, zRrr, insolor
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    sizeof does not take into account the value of the variable It relies only on type . Describe the problem more specifically. - αλεχολυτ 8:43 pm
  • ++ Give a sample code. - StateItPrimitive
  • one
    To determine the length of a string, there is a standard function strlen . - αλεχολυτ

2 answers 2

If the length of the array is in the form of a string, then:

 #include <string.h> char charArr[] = "string"; strlen(charArr); // 6, размер самой строки strlen(charArr) + 1; // 7, размер массива с символом окончания строки 

UPD:

If the length of the array is in the form int array []:

 return sizeof(array)/sizeof(array); 

If this array is dynamic , and there is no end-of-array criterion , then the answer is no way.

  • 2
    The array has a size of 7 bytes. And 6 is the length of the string without a terminating zero. - αλεχολυτ
  • @alexolut ikht, the question is not only the lines, for sure - The_Netos

And what have the pros? If you need to work with strings, then std::string::length to help. If with arrays, then std::vector