Solved

Array size

Posted on 2008-06-22
6
331 Views
Last Modified: 2010-04-21
hi,

is there a built in function to get size of array ? thanks
0
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 21843760
try use
sizeof(arr);
0
 
LVL 53

Expert Comment

by:Infinity08
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 ?
0
 

Author Comment

by:zizi21
ID: 21843844
hi,

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...
0
Technology Partners: 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

by:
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;
0
 

Author Closing Comment

by:zizi21
ID: 31469640
thanks a million
0
 
LVL 53

Expert Comment

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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

730 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