# how to check the length of an array

Page 1 of 1

## 2 Replies - 1145 Views - Last Post: 01 February 2008 - 05:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42199&amp;s=76063bb3930544d13e5feec4eaa0d8f6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 1
• Posts: 113
• Joined: 22-October 07

# how to check the length of an array

Posted 01 February 2008 - 03:58 AM

Hello,
I want to check the length of an array say ARR[100]. Lets assume that after some computations it has 30 values stored in it from ARR[0] to ARR[29]. How do i determine its length?. I am typically expecting an answer of 30.
Thanks,
Is This A Good Question/Topic? 0

## Replies To: how to check the length of an array

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: how to check the length of an array

Posted 01 February 2008 - 04:32 AM

prads, on 1 Feb, 2008 - 04:28 PM, said:

Hello,
I want to check the length of an array say ARR[100]. Lets assume that after some computations it has 30 values stored in it from ARR[0] to ARR[29]. How do i determine its length?. I am typically expecting an answer of 30.
Thanks,

one way could be to keep a counter which will be incremented every time you add a value in array. in another way you can initialize array with some value which you expect never to occur after calculations. and after you finish with your computation, just run a loop till you get that value, the loop counter will give you the length.

I think first way can be good enough for you as second looks like additional logic to be added. I can't think of any other good logic for this right now.

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 5967
• Posts: 29,150
• Joined: 10-May 07

## Re: how to check the length of an array

Posted 01 February 2008 - 05:35 AM

When you init your array, clean it out.

```char ARR[100];
int i=0;

for(i=0;i<=100;i++) {
ARR[i]='\0';
}

```

Now that it's empty, every time that you write a value to ARR, you can check for '\0', & you know you've hit the end of your input.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }