Array size

Posted on 2008-06-22
Last Modified: 2010-04-21

is there a built in function to get size of array ? thanks
Question by:zizi21
  • 3
  • 2
LVL 17

Expert Comment

ID: 21843760
try use
LVL 53

Expert Comment

ID: 21843799
>> 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 ?

Author Comment

ID: 21843844

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...
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 53

Accepted Solution

Infinity08 earned 500 total points
ID: 21843855
>> 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;

Author Closing Comment

ID: 31469640
thanks a million
LVL 53

Expert Comment

ID: 21844128
No problem. Glad to be of assistance :)

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 411
How to create project in Eclipse ? 1 193
Problem to ASCII 1 166
Problem to save 10 147
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now