const char* a;
how do I make sure that string 'a' is null terminated? when a = "abcd" and I do sizeof(a), I get 4. Does that mean its not null-terminated? if it were, I would have gotten 5 ?
const char* a;
how do I make sure that string 'a' is null terminated? when a = "abcd" and I do sizeof(a), I get 4. Does that mean its not null-terminated? if it were, I would have gotten 5 ?
You get 4
because that's the size of a pointer on your system. If you want to get the length of a nul terminated string, you want the strlen
function in the C standard library.
sizeof(a)
gives you the size of the pointer, not of the array of characters the pointer points to. It's the same as if you had said sizeof(char*)
.
You need to use strlen()
to compute the length of a null-terminated string (note that the length returned does not include the null terminator, so strlen("abcd")
is 4, not 5). Or, you can initialize an array with the string literal:
char a[] = "abcd";
size_t sizeof_a = sizeof(a); // sizeof_a is 5, because 'a' is an array not a pointer
The string literal "abcd"
is null terminated; all string literals are null terminated.
sizeof(a)
is sizeof(const char*)
, the size of the pointer. It is not affected by the contents of a
. For that, you want strlen
.
Also, all double-quoted string literals like your "abcd"
in source code are automatically null terminated.
sizeof(a) returns the size of the const char *a
...not the size of what it is pointing to. You can use strlen(a)
to gind the length of the null-terminated string and no, the result of strlen does not include the null-terminator.
The problem here is that you are confusing sizeof()
which is a compile time operation with the length of a string which is a runtime operation. The reason get 4 back when you run sizeof(a)
is that a
is a pointer and the typical size of a pointer in C is 4 bytes. In order to get the length of the string use strlen
.
For the second question, how to make sure a string is null terminated. The only way to definitively do this is to null terminate the string yourself. Given only a char*
there is no way to 100% guarantee it is properly null terminated. Great care must be taken to ensure the the contract between the producer and consumer of the char*
is understood as to who terminates the string.
If you are handed a char array which may or may not have null-terminated data in it, there really isn't a good way to check. The best you can do is search for a null character up to a certian specified length (not indefinitely!). But 0 isn't exactly an unusual byte of data to find in an uninitialzed area of memory.
This is one of the many things about C's defacto string standard that many people dislike. Finding the length of a string a client hands you is an O(n) search operation at best, and a segmentation fault at worst.
Another issue of course is that arrays and pointers are interchangable. That means array_name + 2
is the same as &(array_name[2])
, and sizeof(array_name)
is sizeof(char*)
, not the length of the array.