Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
void main()
  int b[100];
  int a=10;
  } while(a>=2)


The len doesn't work properly. When variable 'a' is initialized a greater number, the output for len is 4. When b[100] is given in place of b[i] in printf(), the answer is 6. How can I get the length of the array correctly for any value of variable 'a'. Please help me. Thanks.
Updated 8-Mar-19 20:27pm
Kornfeld Eliyahu Peter 21-Jun-15 8:50am    
The length of the array b is 100!
I also hardly believe this code goes anywhere (no i defined)...
As for strlen - it checks the number of characters (bytes) from the pointer passed in until the first null character (byte), so it will not work correctly for an int array (as int size is not the same as byte size)...
Frankie-C 21-Jun-15 10:05am    
I think that we have not understood what you're asking.
They already explained you that the function 'strlen()' don't work for what you are doing. Its name is the contraction for 'string+length', and its scope is to find the length of a string of characters, not how many values you have put into a generic array.
When you declare an array it is instanced with the number of elements that you asked for in the declaration, in you case 100 integers.
Your code is not working:
1. Where is defined the variable i ?
2. In the do-while loop you write the result always in b[i] (and i is undefined).
3. In C arrays are zero based, meaning that they starts from 0. For an array of 100 elements the correct indexes are in the range 0-99 included. b[100] is an error!
4. From what we see (points 1,2 & 3) b[100] will never get any value
Please improve your question and your code if you want an answer.

The documentation is always the first point of call. strlen() is for strings is all you need to know.[^]

The way to get the number of members (as distinct from the number of bytes) of your array is this:

int b[100];
int len=sizeof(b)/sizeof(b[0]);[^]
Share this answer
strlen wouldn't work too well for integers, simply because of the way integers are stored.
Assuming 32bit ints, they take four bytes (16 bit integers will use 2 bytes) and for Windows PC's they stored in little endian format - the least significant byte of the integer value will occupy the lowest address.
strlen stops counting the size of a string when it reaches a byte containing '\0' - so if your first integer value is less than 256 it will stop when it spots the second byte, and return a length of 1.
The bigger the numbers, the longer the count will go: if you have 16 bit integers, and your array contains 256, 255, 1777 then the third byte it looks at will be zero, and it will assume that is the end of the string.

You will have to do the search manually, unless pwasser's solution is what you are looking for.
Share this answer
The answer is no.
strlen() is a string function and you can't use it for anything else. ;)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900