C or C++ get array length

C and C++ do not provide a function to directly obtain the length of an array. For character arrays that store strings, a strlen function is provided to obtain the length. How to obtain their lengths for other types of arrays?
One of the methods is to use sizeof(array) / sizeof(array[0]), which is customarily defined as a macro in C language, such as #define GET_ARRAY_LEN(array,len) {len = (sizeof (array) / sizeof(array[0]));} .

In C++, you can use template technology to define a function, such as:

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

In this way, for some simple arrays, you can use this macro or this function to get the length of the array.
 
The following are two Demo programs, one in C language and one in C++:
PS: If the array is a character array that stores strings, that is, a character array that is initialized as a whole with a string enclosed in double quotes.
For example : char a[]=" abcdefg" or char a[]={"abcdefg"}
, the length obtained is the length of the character array, not the length of the corresponding string, and the length of the required string needs to be reduced by one. The
reason is that the characters of the stored string are There is a '\0' character at the end of the array, which needs to be removed.
For char a[]="abcdefg"
sizeof(a)/sizeof(a[0])=8, the length of the string should be reduced by 1.
But for char a[]={'a','b',' c','d','e','f','g'}

sizeof(a)/sizeof(a[0])=7

【C language】


#include <stdio.h>
#include <stdlib.h>
 
#define GET_ARRAY_LEN(array,len) {len = (sizeof(array) / sizeof(array[0]));}
//Define a macro with parameters to store the length of the array in the variable len
intmain()
{
  char a[] = {'1','2','3','4'};
  int len;
 
  GET_ARRAY_LEN(a,len)
//Call the predefined macro to get the length of the array a and store it in the variable len
  printf("%d\n",len);
 
  system("pause");
  return 0;
}

output is 4

【C++】


#include <iostream>
using namespace std;
 
template <class T>
int getArrayLen(T& array) //Use the template to define a function getArrayLen, which will return the length of the array array
{
  return (sizeof(array) / sizeof(array[0]));
}
intmain()
{
  char a[] = {'1','2','3'};
  cout << getArrayLen(a) << endl;
 
  return 0;
}

output is 3

A static array.


For example, int int_ary[5], static arrays know the length of the array at compile time, which is very easy for the compiler to implement.


The most commonly used is probably sizeof:

#define
 countof_macro(x) (sizeof((x)) / sizeof(x)[0])
For C++, templates can be used.


If an array is used as a function parameter, it degenerates into a pointer, but a reference does not. Another problem with using references is that you must know the length of the array when declaring the parameter - what am I going to do if I know? The solution is to use a template, declaring the array size of the array reference as a template parameter:

//Wrong approach: here the formal parameter has been degenerated into a pointer
int countof_func_para(int int_ary[5])
{
    return countof_macro(int_ary);
}
 
//correct way
template<size_t n>
int countof_template(const int (&int_ary)[N])
{
    return N;
}

Second, the dynamic array.


The length of a dynamic array can be specified at runtime, so the compiler does not know its length. But the operating system should always know, otherwise it will know how much memory to free when it is freed. In VC, such a function is provided to obtain the length (bytes) of the memory allocated by calloc, malloc, or realloc. In addition, it is said that Borland C and GCC also implement this function.

size_t _msize(void *);

Note: If the pointer passed in does not point to an area in the heap, the Debug version will have assertion failed.


C++ How to calculate the number of rows and columns of


a two-dimensional array For a two-dimensional array such as: int a[2][5];
find the total number of array elements: sizeof(a) / sizeof(int)
and the number of array rows is: ( sizeof(a) / sizeof(int) )/ ( sizeof(a[0]) / sizeof(int) )
to find the number of array columns: sizeof(a[0])/sizeof(int)

#include <iostream>  
using namespace std;  
intmain()  
{  
    int a[2][3]={{1,2,3},{1,2,3}};  
    int m=sizeof(a[0])/sizeof(int);  
    int n=(sizeof(a)/sizeof(int))/(sizeof(a[0])/sizeof(int));  
    cout<<n<<' '<<m;  
    return 0;  
}

For vector containers:

vector<vector<int> > matrix
int row=matrix.size();//行数
int collor=matrix[0].size();//列数





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325932140&siteId=291194637