Rob
Rob

Reputation: 1

Empty String Input as bit representation

This must be one of the most silly questions ever asked, but my brain seems not to be working at all at the moment. For testing purpose I need the bit representation of the empty string to do some hardware debugging.

In other words, the input to a C-function would be the empty string, ie, "", now I would like to know how I can represent the empty string as a 64-bit value. Is this just a sequence of 64 Zeros or do I miss here something?

Thanks!

Upvotes: 0

Views: 2092

Answers (4)

Joe Tyman
Joe Tyman

Reputation: 1417

The first char would be '\0', which would be a null terminator. In the memory the previous char from what the was before it was set to NULL would still be there. So lets string = Hello World, then string = \0. The memory would look like '\0', 'e', 'l', 'l', 'o', '', 'W', 'o', 'r', 'l', 'd', '!'. So if you initialize string to null. It would first be a char of \0 and the rest would be bits set to zero.

Upvotes: 0

pmg
pmg

Reputation: 108978

What a "C-string" is, is an array of characters with a zero-element; which decays to a pointer to its first element in most contexts. Also, the converse is usually accepted: a pointer to char is (often) assumed to be a pointer to (a part of) an array of characters which has a (subsequent) zero-element.

If you interpret "empty string" as array of 0 elements, there really is no concept of that in the C language.
If you interpret "empty string" as array of N elements of which the first element is a 0, the bit representation of that 1st element is CHAR_BITbinary 0's (usually 8).
If you interpret "empty string" as pointer to NULL, the bit representation of that pointer is implementation defined, but comparing it to 0 must yield "true".

/* ERRATA: where it says "is a string" please read "can be interpreted as a string" */
char arr_string[10]; /* if any element is 0, this is a string */
char *ptr_string; /* if it points to a valid object accessible as `char`
                  ** and a subsequent valid char is 0, this is a string */

strcpy(arr_string, "foobar"); /* arr_string[6] == 0 */
ptr_string = arr_string;

test_string_empty(arr_string);      /* not empty */
test_string_empty(ptr_string + 6);  /* empty */
test_string_empty(NULL);            /* unknown */

Upvotes: 2

philant
philant

Reputation: 35836

C string termination character is \0 (all bits set to zero). The remaining bits won't necessary always be zeroes: if the string is emptied via setting its first char to \0, the other chars won't change. A sequence of 64 bits set to zero is one representation of an empty C string.

Upvotes: 0

peakxu
peakxu

Reputation: 6675

Yes, the string terminator has a value of 0. The 64-bit representation would indeed be 64 zeros.

Upvotes: 0

Related Questions