Provided the char array is null terminated,
char chararray[10] = { 0 };
size_t len = strlen(chararray);
Answer from Daniel A. White on Stack OverflowProvided the char array is null terminated,
char chararray[10] = { 0 };
size_t len = strlen(chararray);
If you have an array, then you can find the number of elements in the array by dividing the size of the array in bytes by the size of each element in bytes:
char x[10];
int elements_in_x = sizeof(x) / sizeof(x[0]);
For the specific case of char, since sizeof(char) == 1, sizeof(x) will yield the same result.
If you only have a pointer to an array, then there's no way to find the number of elements in the pointed-to array. You have to keep track of that yourself. For example, given:
char x[10];
char* pointer_to_x = x;
there is no way to tell from just pointer_to_x that it points to an array of 10 elements. You have to keep track of that information yourself.
There are numerous ways to do that: you can either store the number of elements in a variable or you can encode the contents of the array such that you can get its size somehow by analyzing its contents (this is effectively what null-terminated strings do: they place a '\0' character at the end of the string so that you know when the string ends).
C sizeof char* array - Stack Overflow
arrays - Sizeof(char[]) in C - Stack Overflow
c - size of character array and size of character pointer - Stack Overflow
size of char array in c
Videos
Note the difference between sizeof and strlen. The first is an operator that gives the size of the whole data item. The second is a function that returns the length of the string, which will be less than its sizeof (unless you've managed to get string overflow), depending how much of its allocated space is actually used.
In your example
char name[]="123";
sizeof(name) is 4, because of the terminating '\0', and strlen(name) is 3.
But in this example:
char str[20] = "abc";
sizeof(str) is 20, and strlen(str) is 3.
As Michael pointed out in the comments the strings are terminated by a zero. So in memory the first string will look like this
"123\0"
where \0 is a single char and has the ASCII value 0. Then the above string has size 4.
If you had not this terminating character, how would one know, where the string (or char[] for that matter) ends? Well, indeed one other way is to store the length somewhere. Some languages do that. C doesn't.
firstname is a char array carrying a trailing 0-terminator. lastname is a pointer. On a 64bit system pointers are 8 byte wide.
sizeof an array is the size of the total array, in the case of "bobby", it's 5 characters and one trailing \0 which equals 6.
sizeof a pointer is the size of the pointer, which is normally 4 bytes in 32-bit machine and 8 bytes in 64-bit machine.
A char array has the terminating \0 byte at the last index, so you can easily iterate through it. I can access the array by using it's address, stored in a pointer variable. The pointer is just a number, and by adding digits i can get any array index i want.
Integer arrays do not have a terminating byte and there is no information about the size in the pointer address. So how does for example free() know what to free?
4 isn't the size of the string, because samplestring isn't a string. It's a char*, whose size is (on your platform) 4, divided by 1 (size of char) is, correctly, 4.
In C++, you'd use std::string and the length() method.
In C, you'd use strlen which takes as parameter a NULL-terminated char pointer.
There are two ways of making a string constant, and your technique only works on the first one. The first one makes an array of characters which you can get the size of at compile time, and the other creates a pointer to an array of characters.
char samplestring[] = "hello";
char * samplestring = "hello";
Trying to take the size of the second case the way you're doing it just gives you the size of a pointer. On a 32-bit build the size of a pointer is 4 characters, i.e. a pointer takes the same amount of memory as 4 characters.
The following will always give the correct length for a properly null-terminated string, but it's slower.
mysize = strlen(samplestring);
The first create an array of 200 chars and initialize its contents with {'N','a','m','e','\0', ... } (the rest is filled with NULs).
The second create an array just large enough to hold "Name", that is, 5 chars (one more for the NUL)
Besides, you should use double quotes for string literals in C and C++.
First you meant:
char name[200] = "Name";
char name[] = "Name";
(strings are quoted with " not with ')
Then the difference is the storage reserved for name object in the first case is 200 bytes and in the second case is 5 bytes (that is the string length + 1 byte for the trailing null character).
By convention C strings are 'null-terminated'. That means that there's an extra byte at the end with the value of zero (0x00). Any function that does something with a string (like printf) will consider a string to end when it finds null. This also means that if your string is not null terminated, it will keep going until it finds a null character, which can produce some interesting results!
As the first item in your array is 0x00, it will be considered to be length zero (no characters).
If you defined your string to be:
char a[7]={0xdc,0x01,0x04,0x00};
e.g. null-terminated
then you can use strlen to measure the length of the string stored in the array.
sizeof measures the size of a type. It is not what you want. Also remember that the string in an array may be shorter than the size of the array.
If you are expecting 4 as output then try this:
char a[]={0x00,0xdc,0x01,0x04};