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).
Videos
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};
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?
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).