Get array lengtn in C and CPP

Source: Internet
Author: User


How to get the length of an array

C and C ++ do not provide functions that directly obtain the length of an array. For character arrays that store strings, A strlen function is provided to obtain the length, so how can we get their lengths for arrays of other types? One of the methods is to use sizeof (array)/sizeof (array [0]). In C language, it is used to define it as a macro, for example, # define get_array_len (array, Len) {Len = (sizeof (array)/sizeof (array [0]);}. In C ++, you can use the template technology to define a function, for example:

template <class T>int getArrayLen(T& array){return (sizeof(array) / sizeof(array[0]));}

In this way, you can use this macro or this function to obtain the length of the array for different types of data groups. The following are two demo programs, one in C and one in C ++:

P.s: If the array is a character array that stores strings, the obtained length must be reduced by one, that is, for macro definition: # define get_array_len (array, Len) {Len = (sizeof (array)/sizeof (array [0])-1) ;}, for Function Definition:

template <class T>int getArrayLen(T& array){return (sizeof(array) / sizeof(array[0]) - 1);}

The reason is that there is a '\ 0' character at the end of the character array storing the string. You need to remove it.

[C Language]

# Include <stdio. h> # include <stdlib. h> # define get_array_len (array, Len) {Len = (sizeof (array)/sizeof (array [0]);} // defines a macro with parameters, store the array length in the variable Len int main () {char a [] = {'1', '2', '3', '4'}; int Len; get_array_len (A, Len) // call a predefined macro to get the length of array A and store it in the variable Len printf ("% d \ n", Len ); system ("pause"); Return 0 ;}

[C ++]

# Include <iostream> using namespace STD; Template <class T> int getarraylen (T & array) {// use the template to define a function getarraylen, this function will return the length of the array return (sizeof (array)/sizeof (array [0]);} int main () {char a [] = {'1 ', '2', '3'}; cout <getarraylen (a) <Endl; return 0 ;}

In addition, if an array is passed as a parameter in the C ++ function, the array will be degraded into a pointer, therefore, the length of the array is unknown (the array here refers to static instead of new)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.