devxlogo

Calculating the Size of an Incomplete Array

Calculating the Size of an Incomplete Array

An incomplete array declaration can appear in a function’s parameter list. For example:

   int count(const char s[]); 

The declaration of s doesn’t include the array’s size. Can count() use the operator sizeof to calculate the size of s? No, it can’t. The compiler implicitly transforms an array into a pointer. Therefore, sizeof returns a pointer’s size, not an array’s size. One way to obtain the array’s size is to pass an additional argument that holds the number of elements in the array:

   int count(const char s[], int arr_size); 

However, this solution is error prone. A better solution is to pass a vector and call its size() member function:

   int cout(const vector  &s )  {    return s.size();  }
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist