dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
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: Beginner
Mar 24, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Sizeof Operator

Question:
When declaring an unbounded array in my function's parameter list, int a[], can I use the sizeof() operator to determine the array's length at runtime?

Answer:
No, you can't use sizeof for this purpose. The problem is that the compiler silently converts an array parameter to a pointer. Therefore, sizeof will return the size of that pointer, not the size of the array.

You can use a vector object instead of a built-in array. A vector stores the number of its elements in an internal member. You can get the vector's size by calling the size() member function:

void func( vector  & v)
{
 int sz = v.size();
}
If you're programming in C, you can pass the number of elements in another parameter:
void func( int arr[], int arr_size);
DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date