strlen() is used to get the length of a string stored in an array.
sizeof() is used to get the actual size of any type of data in bytes.
Besides, 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 null character. It counts the number of characters before it finds the null character. In other words, it gives you the length of a C-style null-terminated string.
The two are quite different. In C++, you do not 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).
strlen() is used to get the length of a string stored in an array.
sizeof() is used to get the actual size of any type of data in bytes.
Besides, 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 null character. It counts the number of characters before it finds the null character. In other words, it gives you the length of a C-style null-terminated string.
The two are quite different. In C++, you do not 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).
sizeof is not a method. It is a compile-time construct that determines the amount of memory a particular type or a variable occupies. strlen, on the other hand, is a function that counts the number of consecutive non-zero char values starting at the specified location in memory (which happens to be the same as determining the length of a zero-terminated C string).
sizeof VS. strlen
c - How to get the string size in bytes? - Stack Overflow
Is it possible to get sizeof string in array of string literals?
sizeof a string
Videos
#include<stdio.h>
#include<string.h>
main()
{
char str[10] = "clue";
strcpy(str, "no ");
strcat(str, "more");
printf("%d", sizeof(str) - strlen(str));
}In the following code, I expected the output to be 1, since if I'm not wrong, the length of str changes twice after it's initialized, and in the end I have ''no more'' string, strlen for which is 7 and sizeof for which is 8, since \0 is also included in the count. I get 3 for some reason though.
You can use strlen. Size is determined by the terminating null-character, so passed string should be valid.
If you want to get size of memory buffer, that contains your string, and you have pointer to it:
- If it is dynamic array(created with malloc), it is impossible to get it size, since compiler doesn't know what pointer is pointing at. (check this)
- If it is static array, you can use
sizeofto get its size.
If you are confused about difference between dynamic and static arrays, check this.
Use strlen to get the length of a null-terminated string.
sizeof returns the length of the array not the string. If it's a pointer (char *s), not an array (char s[]), it won't work, since it will return the size of the pointer (usually 4 bytes on 32-bit systems). I believe an array will be passed or returned as a pointer, so you'd lose the ability to use sizeof to check the size of the array.
So, only if the string spans the entire array (e.g. char s[] = "stuff"), would using sizeof for a statically defined array return what you want (and be faster as it wouldn't need to loop through to find the null-terminator) (if the last character is a null-terminator, you will need to subtract 1). If it doesn't span the entire array, it won't return what you want.
An alternative to all this is actually storing the size of the string.
I know you can do this:
const char str[] = "string"; size_t len = sizeof(str);
but if I had multiple strings in an array, how would I use sizeof on one of the strings:
const char *strarray[] = {"string1", "string2", "string3"};
sizeof(strarray); //this equals size of all pointers
sizeof(strarray[0]); //this equals pointer size
sizeof(*strarray[0]); //this equals char sizeIs this possible in C or do I have to use strnlen?