Re: [Dev-C++] problem with strlen( )...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-11-17 02:50:43
|
The function strlen() is for working with strings and not general arrays.
You are lucky that the program didn't lock up or crash when you tried it. It
works by detecting the NULL terminator on the end of the string. As your
arrays are of fixed size then the sizeof operator will give you the numbers
you need, but that won't work for an array who's size is unknown at compile
time (i.e. dynamically allocated arrays). Don't forget that sizeof returns
the number of 'bytes' in a variable, object or array (calculated at compile
time).
Maybe a vector would be better suited to your needs?
Jason.
----- Original Message -----
From: "Julio Lajara" <ju2...@ya...>
To: <dev...@li...>
Sent: Saturday, November 17, 2001 2:31 AM
Subject: [Dev-C++] problem with strlen( )...
> #include <iostream.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main()
> {
>
> char array[10]= {1,2,3,4,5,6,7,8,9,10};
> char array2[2]= {1,2};
> char array3[3]= {1,3,30};
>
> cout << "\n";
>
> for(unsigned short int i = 0; i < 10; i++)
> cout << "array " << i+1 << ": " <<
> static_cast<int>(array[i]) << "\n";
>
> cout << "array length: " << strlen(array) <<
> "\n";
> cout << "array2 length: " << strlen(array2) <<
> "\n";
> cout << "array3 length: " << strlen(array3) <<
> "\n\n";
> system("PAUSE");
> return 0;
> }
>
> why does strlen return 11 for array, 13 for array2 and
> 7 for array3??
>
> =====
> Julio
>
> __________________________________________________
> Do You Yahoo!?
> Find the one for you at Yahoo! Personals
> http://personals.yahoo.com
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|