Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Array size

Posted on 2008-06-22
Medium Priority
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...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 53

Accepted Solution

Infinity08 earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

571 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