3

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?

GamerCoder
  • 139
  • 6
  • 4
    Yes you can have a "string" without the null-terminator. But it can't be used by any function expecting the string to be null-terminated (which is all standard C functions). You need to invent your own functions to be able to handle it, and then always include *both* the actual length and array sizes in the calls. – Some programmer dude Oct 15 '21 at 11:35
  • 1
    By definition a C string is terminated by a null character. If you don't want a null character as a terminator, you need to store the length of your "strings" in some way. What are you _actually_ trying to achieve? This looks like an [XY problem](https://xyproblem.info/) – Jabberwocky Oct 15 '21 at 11:35
  • Manipulating string-like data that is not null-terminated is common. `read` and `fread` are examples of functions that can read text into a buffer that is not necessarily null terminated. When you have not-necessarily-null-terminated text data, you need to keep track of the length some other way, usually with a separate `len` variable. You can print a not-necessarily-null-terminated string, whose length you know, with `printf("%.*s", len, str)`. – Steve Summit Oct 15 '21 at 11:40
  • "string" is not a type, it is a data structure embedded into array of `char`s. The invariant of this structure is having `0` byte at the end. So there are no string without nul teminators by definition – tstanisl Oct 15 '21 at 11:41
  • Although some compilers will accept it as an extension, `char cArray[100] ={};` is not a valid declaration. Use `char cArray[100];` if you don't care about the initial contents, or `char cArray[100] ={ 0 };` if you want it initially zero-filled (which makes the contents a string -- or several of them, if you want to be technical). – John Bollinger Oct 15 '21 at 11:44

6 Answers6

11

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]; // the original had the illegal initializer = {};

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

Yes, that's perfectly reasonable (memset(cArray, '*', 100);). 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.

pmg
  • 106,608
  • 13
  • 126
  • 198
8

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • What if I just put 'char array without null' into functions have to do with string? They just keep going and going until I force stop the process? – GamerCoder Oct 15 '21 at 11:46
  • 1
    @GamerCoder, the language specification does not define the results of such operations. In practice, you usually get unwanted results of one kind or another, which you may or may not immediately notice. It is much more likely that the program crashes than that it continues but the function call fails to return. – John Bollinger Oct 15 '21 at 11:51
  • 1
    @GamerCoder You would invoke undefined behavior if you pass a non-string to any function expecting to handle a string. – Andrew Henle Oct 15 '21 at 11:52
  • @GamerCoder You can try this experiment: `int a = 0; int b = 1684234849; char c[4] = "oops"; int d = 2054781047; int e = 0; printf("%s\n", c);`. Here, `c` is a character array that's *not* null-terminated. When you try to print it using `printf %s`, it sails off the end. On my machine, it prints `oopsabcd`. (This is not guaranteed, of course.) – Steve Summit Oct 15 '21 at 12:42
7

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.

alenada99
  • 356
  • 1
  • 6
6

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 :)

msimonelli
  • 393
  • 1
  • 6
  • 3
    A question such as this demands precision to avoid creating confusion. To that end, a `char *` *is not* a string. Some such pointers *point to* characters in strings, but those pointers are not themselves strings. – John Bollinger Oct 15 '21 at 11:48
2

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 */
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
2

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?

Lundin
  • 195,001
  • 40
  • 254
  • 396