How can you quickly find the number of elements stored in a
a) static array b) dynamic array ?

Answer Posted / jaroosh

a) sizeof(array)/sizeof(element)
b) you cant (well, you can, eg. by using realloc, counting
from 0 up, and checking the result etc. but this is absurd),
its best to keep track somehow how many elements are there
in the array, but the BEST solution is...do NOT use dynamic
arrays at all, use std containers instead like vector.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Should the this pointer can be used in the constructor?

558


Is c or c++ more useful?

581


What is name hiding in c++?

618


Define stacks. Provide an example where they are useful.

583


Can c++ do everything c can?

608






What are the four partitions in which c++ compiler divides the ram?

704


Can you pass a vector to a function?

540


Do you need a main function in c++?

561


What is difference between c++ and c ++ 14?

581


What is dev c++ used for?

607


What are the new features that iso/ansi c++ has added to original c++ specifications?

588


Explain how to initialize a const data member.

598


If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class?

612


What is the benefit of c++?

605


What are containers in c++?

573