Hi Friends,
Can anyone tell me what is the difference between strlen() and sizeof()
Thanks in Advance!!!!
Hi Friends,
Can anyone tell me what is the difference between strlen() and sizeof()
Thanks in Advance!!!!
The difference is:
strlen() is used to get the length of an array of chars / string. sizeof() is used to get the actual size of any type of data in bytes.
Consider there's this array of char:
strlen will output 12 while sizeof 13. The NULL character at the end of the string won't be calculated in strlen.Code:const char* A = "Hello World!\0";
ERROR: Brain not found. Please insert a new brain!
“Do nothing which is of no use.” - Miyamoto Musashi.
That's not really accurate and can lead to a misunderstanding, especially since sizeof(A) will not give you 13.
sizeof() is a compile-time expression giving you the size of a type or a variable's type. It doesn't care about the value of the variable.
strlen() is a function that takes a pointer to a character, and walks the memory from this character on, looking for a NUL character. It counts the number of characters before it finds the NUL character. In other words, it gives you the length of a C-style NUL-terminated string.
The two are entirely different in purpose and have nearly nothing to do with each other. In C++, you shouldn't need either very much; strlen() is for C-style strings, which should be replaced by C++-style std::strings, whereas the primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn't use in C++ (use new, std::copy(), and std::fill() or constructors).
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
No, sizeof this will give you 13:And sizeof this would give you 14 since it's double-null-terminated:Code:const char A[] = "Hello World!";But sizeof this will most likely give you 4 or 8 depending on the size of a pointer:Code:const char A[] = "Hello World!\0";Code:const char *A = "Hello World!\0";
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Your comment here is inapplicable to string literals - the type (and therefore size) of a string literal is related to its value. However, the relationships between string literals, arrays of char, and pointers to char are technically an anomaly in C (and C++) - no other types are affected by this anomaly.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
What will be the output of strlen(A) ??Code:No, sizeof this will give you 13: Code: const char A[] = "Hello World!";
> What will be the output of strlen(A) ??
Try it for yourself!
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hey,
The ''sizeof" is a keyword of operator, while the strlen() is a function. The sizeof returns the length of a varible or data type, in other words, how many bytes this data block of type occupied. It can used on all dada types. The strlen() can only operate on a string and returns the number of characters before '\0' of a string.
For example:
char a[100] = "abc\0ddddd";
The result of sizeof(a) will be 100 while the strlen(a) will be 4.
Carle
_____________________
Free chat software for you
Microsoft Certified Partner
Thanks A lot Carle...
Now I am much more clear...
Yaaaa Sebastiani...
you are correct....
Thanks...
Last edited by subhashish1213; 09-21-2009 at 11:51 PM.
haha,
yes, you r right. I made a mistake~~~~~~~~~
carle
________________
Free chat software for you
Microsoft Certified Partner