Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof for a null terminated const char*

Tags:

c

string

char

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 ?

like image 778
hari Avatar asked Oct 29 '10 17:10

hari


People also ask

Is const char * null terminated?

Are C constant character strings always null terminated without exception? Yes - although your question sounds like you suspect otherwise... Yes, all string in C are represented by string 0 terminated. @AditiRawat '\0' is just a char with value 0, it can be checked against 0.

What is the size of null terminator in C?

In all modern character sets, the null character has a code point value of zero. In most encodings, this is translated to a single code unit with a zero value. For instance, in UTF-8 it is a single zero byte. However, in Modified UTF-8 the null character is encoded as two bytes: 0xC0, 0x80.

Does sizeof include null character?

Data types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.

How do you find the length of a null-terminated string?

strlen(s) returns the length of null-terminated string s. The length does not count the null character.


4 Answers

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.

like image 186
James McNellis Avatar answered Sep 18 '22 16:09

James McNellis


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.

like image 37
jer Avatar answered Sep 21 '22 16:09

jer


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.

like image 25
JaredPar Avatar answered Sep 19 '22 16:09

JaredPar


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(a) is sizeof(char*), not the length of the array.

like image 33
T.E.D. Avatar answered Sep 21 '22 16:09

T.E.D.