Solved

Array size

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

is there a built in function to get size of array ? thanks
0
Comment
Question by:zizi21
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 (http://en.wikipedia.org/wiki/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 and use conditional statements in the C programming language.

760 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

18 Experts available now in Live!

Get 1:1 Help Now