Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Mar 23, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 <char> &s )
    return s.size();
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date