2 Replies - 879 Views - Last Post: 01 February 2008 - 05:35 AM Rate Topic: -----

#1 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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,
prads
Is This A Good Question/Topic? 0
  • +

Replies To: how to check the length of an array

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: how to check the length of an array

Posted 01 February 2008 - 04:32 AM

View Postprads, 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,
prads


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.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,450
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1