NO

Author Topic: sizeof X strlen  (Read 2909 times)

emneto

  • Guest
sizeof X strlen
« on: September 07, 2005, 04:37:36 PM »
Hi all,

I don't now if this is the right place to this post but here we go...

Look at the little program below:

int main( int argc, char *argv[ ] ) {

   unsigned char arr[ ] = "the brown fox...";

   // sets size to 17 !!! ???
   unsigned int size = sizeof( arr ) / sizeof( unsigned char );

   // sets size to 16 !!! ???
   size = strlen( ( const char* )arr );

        return 0;

}

As seem in the coments, whem I use sizeof I get "size = 17" and
when I use strlen I get "size = 16"...
I this correct and, If it is, why? :?

Cheers

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
sizeof X strlen
« Reply #1 on: September 07, 2005, 05:08:08 PM »
When you use sizeof() in this case the nul character, that terminates the string, will also be counted - strlen() will not count it.

Pelle
/Pelle

emneto

  • Guest
sizeof X strlen
« Reply #2 on: September 07, 2005, 06:38:55 PM »
Quote from: "Pelle"
When you use sizeof() in this case the nul character, that terminates the string, will also be counted - strlen() will not count it.

Pelle


Yeah!! Y're rigth, I don't know wat I was thinking when I post this message. :oops:

Thanks and sorry.