Home > Blockchain >  In C, Is it possible to create a string without '\0(null)'?
In C, Is it possible to create a string without '\0(null)'?

Time:10-16

I mean, as far as I know, a string would 'end' when 'null value(\0)' appears.

And many functions have to do with string return value and escape as soon as it meets 'null value' in string.

But, what if there's no null value in string?

Is it even possible to create such string?

Like, is it possible to create an array

char cArray[100] ={};

and fill all the indexes out without 'null'?.

If so, what's the best way to 'indicate the end of string that doesn't have null value at all'?

Is it even 'a string' if there's no null in it?

CodePudding user response:

You can totally have a char array without the null terminator (it is like an int array after all), but it is NOT a string (by definition) and you won't be able to use all the standard C functions related to strings since they all rely on the presence of the terminator.

In the end you would have to treat the array of characters as a generic array.

CodePudding user response:

Is it even possible to create such string?

You can have arrays of char without a zero element. Those are not strings.

Like, is it possible to create an array

char cArray[100] ={};

and fill all the indexes out without 'null'?.

Yes, that's perfectly reasonable. But cArray (or any part of it) will not be a string.

If so, what's the best way to 'indicate the end of string that doesn't have null value at all'?

If you want to work with sequences of bytes without an indicator, you need to keep count separately.

char NotAString[6] = "abcdef";
size_t n = 6;
// remove the 'f' from NotAstring
n = 5;
// remove the 'b'
memmove(NotAString   1, NotAString   2, 3);
n -= 1;
// the first `n` (4) bytes of NotAString are now 'a', 'c', 'd', and 'e'
// don't care about bytes 5 and 6

Is it even 'a string' if there's no null in it?

No.

CodePudding user response:

But, what if there's no null value in string?

Then it's not a string. A "string" is defined by 7.1.1 Definitions of terms, pararaph 1 in the (draft) C11 standard:

A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.

Without that "null character", the array is NOT a "string".

Full stop.

CodePudding user response:

This really depends on what your end goal here is.

But, what if there's no null value in string?

Any standard C library functions that take a char* argument (a string) expect a null terminator. If you don't add one at the end of the string, whatever function takes your unterminated string will just keep reading beyond the end of your string, which is UB.

Is it even possible to create such string?

Well yes, but it won't be very useful. You could store the length and the string separately and do something like this:

char myString[100] = { ... } // unterminated string
size_t myStringLength = 100;

for (int i = 0; i < myStringLength; i  )
{
    // do stuff
}

But this only works in your own implementations, as again, no standard C library functions support this.

is it possible to create an array and fill all the indexes out without 'null'?.

Again, yes. An array is just an array, and this one would effectively just be an array of "numbers". As Jabberwocky mentioned, a string is defined by having a null terminator at the end.

If so, what's the best way to 'indicate the end of string that doesn't have null value at all'? With the standard C library functions, there is no way to do this.

To conclude:

Is it even 'a string' if there's no null in it?

No :)

CodePudding user response:

It's quite common to work with non-null-terminated text data in C. You just have to be careful with your terminology, because such data is not, formally, a "string".

If you have some text data that's not terminated by a null character, you obviously have to keep track of its length some other way. The most common way is to keep a separate variable (often called n, size, sz, length, or len) containing the length.

For most operations which work on strings, there is a corresponding way to work with non-null-terminated text data. Here is a table:

operation conventional string counted text
read from file fgets fread, read
copy strcpy memcpy
compute length strlen n/a
write to file fputs fwrite, write

You can create an array containing a non-null-terminated "string" using a trick:

char nonstr[5] = "hello";

Normally, when you initialize a char array with a string literal, the C compiler picks the size, and includes space for the null terminator. That is, when you write

char str[] = "hello";

the array comes out with a size of 6. But when you specify an explicit size, and when it's "too small" by exactly 1, you get a non-null-terminated string. To summarize:

char  error[3] = "hello";    /* error: array too small */
char nonstr[5] = "hello";    /* special case: no null */
char string[6] = "hello";    /* normal case: right size, including null */
char extra[10] = "hello";    /* extra space (could contain larger string later) */
char default[] = "hello";    /* default case: compiler picks size 6 */

CodePudding user response:

A string in C is a character array terminated with zero.

But, what if there's no null value in string?

Then it isn't a string, but a raw data array.

Is it even possible to create such string?

No, since it wouldn't be a string then.

is it possible to create an array and fill all the indexes out without 'null'?.

Sure. Character arrays are not necessarily strings.

Is it even 'a string' if there's no null in it?

No.

Also check How should character arrays be used as strings?

  • Related