views:

134

answers:

4

I'm trying to count the number of chars in a char array including the space until the end of the string.

The following compiles but doesn't return the correct value, I'm trying to use pointer arithmetic to interate through my array.

   int numberOfCharsInArray(char* array) {
       int numberOfChars = 0;
       while (array++ != '\0') {
           numberOfChars++;
       }
       return numberOfChars;
   }

Many thanks.

Obviously I'm trying to get the equivalent of length() from cstring but using a simple char array.

Of course if my original array wasn't null terminated this could cause a very big value to return (I guess).

+6  A: 

To access the char pointer by the pointer you need to dereference the pointer. Currently you are comparing array ( an address) with '\0'

You can fix your code like:

int numberOfCharsInArray(char* array){
 int numberOfChars = 0;
   while (*array++){
       numberOfChars++;
   }
   return numberOfChars;
}

The cstring function you are imitating is strlen not length.

EDIT:

To know how the condition in the while works you can see this thread.

codaddict
He's probably talking about MFC's CString class' length() method. Also, I can't see any double increment? The dereference omission seems key here.
Graham Perks
OP edited his post within 5 min :)
codaddict
**@Downvoter: The original post by OP was incrementing array twice once in the while condition and once in the body. Later OP edited the post. @OP: Please don't do such things. It makes some answers look bad**
codaddict
Since your (good) answer does not explain why you also removed the non-equality test against '\0', I'll add (just in case): The != '\0' part of the test is not needed as '\0' is in fact equal to numeric 0, which translates to false, while all other characters are non-zero and hence true.
gspr
sorry, I posted it and immediately saw the double increment I thought I'd got to the edit button in time for nobody to see it. You guys are just too fast!
Zac
What if the input argument, `array`, is `NULL`?
ArunSaha
Have you ever tried passing `NULL` to `strlen` ?
codaddict
Well, why should it matter what `strlen` does? Per the problem definition by the OP ("count the number of chars in a char array..."), there is no mention whatsoever to `strlen`. I know `strlen` is a *similar* function, but I am not sure why its behavior need to be mimicked here. No offense, its just a thought.
ArunSaha
+2  A: 

When you write array++ != '\0' you check if the memory address array is '\0'. Try this instead:

int numberOfCharsInArray(char* array){
int numberOfChars = 0;
while (*array != '\0'){
   numberOfChars++; array++;
   }
return numberOfChars;
}

Edit: Oops, codaddict was faster and his code more elegant.

gspr
+2  A: 

Perhaps I'm missing something, but why not just:

int numberOfCharsInArray(char* array) {
  return strlen(array);
}  

...or even:

int numberOfCharsInArray(char* array) {
  return std::string(array).length();
}
John Dibling
I think OP is trying to make his own implementation of a string length function, so that would be cheating.
gspr
@gspr: You might be right, but in that case I wonder why in the wide world would he want to reinvent that wheel?
John Dibling
erm, nope I just wasn't aware of the function (I'm new).
Zac
A: 
static const size_t maxExpectedChars = 4 * 1024; // Max chars expected, e.g. 4K    
size_t numberOfCharsInArray( char * array) {
    if( !array ) { return 0; }         // A non-existing string has `0` length
    size_t charsSoFar = 0;
    while ( *array ) {
        charsSoFar += 1;
        if( charsSoFar == maxExpectedChars ) { break; }  // Stop runaway loop
        ++array;
    }
    return charsSoFar;
} 
ArunSaha