Is there a function used to calculate the length of an array. For instance, if someone enters John, the function outputs 4. Or is it necessary to write a function to do it, and how is it done. (This is not homework this time, just want to know for my job).
Slate2006 0 Newbie Poster
Recommended Answers
Jump to PostIf it's actually an array, there are two common ways to find the size. The first is a template that converts an array of type T to an array of type char, which can then be used as the operand to sizeof to get the number of elements:
Jump to Post> a template that converts an array of type T to an array of type char, which can then be used as the operand to sizeof to get the number of elements:
template <typename T, size_t N> char (&array(T(&)[N]))[N];
another way to use the same template idea …
Jump to Post>which i find slightly more readable
If it were readable, it wouldn't be a proper template definition. :D
All 11 Replies
vmanes 1,165 Posting Virtuoso
Narue 5,707 Bad Cop Team Colleague
Slate2006 0 Newbie Poster
vmanes 1,165 Posting Virtuoso
Slate2006 0 Newbie Poster
vijayan121 1,152 Posting Virtuoso
Narue 5,707 Bad Cop Team Colleague
Calicoder -4 Newbie Poster
Narue commented: It took you four years to come up with the same solution vijayan did in 2007. Grats on a pointless bump. -4
adityatandon 23 Junior Poster in Training
zeroliken 79 Nearly a Posting Virtuoso
Sebelius 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.