• C

Array size


is there a built in function to get size of array ? thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try use
>> is there a built in function to get size of array ?

Depends what you mean by that. If you mean you want to get the size of a statically defined array, you can do this :

        Object array[10];
        int size = sizeof(array) / sizeof(array[0]);

and size will have the value 10.

This ONLY works for statically allocated arrays though.

Can you be a bit more specific ?
zizi21Author Commented:

i have an array which is dynamically allocated...

i want to know the number of elements..currently, i am looping through the array to find the number of elements...maybe..., i thought, there is a built in function...
SolarWinds® IP Control Bundle (IPCB)

Combines SolarWinds IP Address Manager and User Device Tracker to help detect IP conflicts, quickly identify affected systems, and help your team take near instantaneous action. Help improve visibility and enhance reliability with SolarWinds IP Control Bundle.

>> i have an array which is dynamically allocated...

There is no way of knowing the size of a dynamically allocated array from the pointer alone. So, you'll have to store the size of the array somewhere, either using a separate integer value, or by using a terminator in the array. The latter is commonly used for char arrays (strings), where a standard strlen function exists that gets the length of the string by looking for the terminator.

The most common way is to just store the size separately :

        int *array = (int*) calloc(10, sizeof(int));
        int size = 10;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zizi21Author Commented:
thanks a million
No problem. Glad to be of assistance :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.