2

I am having trouble wrapping my brain around null terminators and non-null terminating arrays.

Let's say I have two declarations:

const char *string = "mike";

and

const char string[4] = {'m', 'i', 'k', 'e'};

I understand the first declaration is because in C, a character array is null terminated because it is a defined to be a contiguous block of characters in memory terminated by a NULL and I can check this with strlen.

The problem that I'm having is understanding declarations like the second, with no null terminator.

How can I check for validity of a string with no null terminator? (as in, what if there are additional values in the array?)

7
  • 3
    What do you mean by "validity" exactly? And why do you say it's a "string"? What's the definition of a "string". Commented Feb 11, 2016 at 2:14
  • Perhaps you meant to write {'m', 'i', 'k', 'e'} instead. Commented Feb 11, 2016 at 2:14
  • I bet const char string[4] = {'m', 'i', 'k', 'e'}; would work better Commented Feb 11, 2016 at 2:14
  • 1
    There is no string. It is not the string that ends, only yourself. Commented Feb 11, 2016 at 2:15
  • Ah yes my bad, I meant string[4] = {'m', 'i', 'k', 'e'}; Commented Feb 11, 2016 at 2:15

3 Answers 3

4

How can I check for validity of a string with no null terminator?

You need to know array bounds in order to see if a null-terminated string is contained within the bounds. Here is how you can do that in your example:

const char string[4] = {'m', 'i', 'k', 'e'};
int good = 0;
for (int i = 0 ; i != sizeof(string) ; i++) {
    if (string[i] == '\0') {
        good = 1;
        break;
    }
}
if (good) {
    printf("String '%s' is null-terminated.\n", string);
} else {
    printf("String is not null-terminated; cannot print.\n");
}

Although C library provides support only for null-terminated strings, you could use character arrays without null termination as long as you have access to their size (i.e. it's an array, not a pointer). For example, you could print your array like this:

printf("'%.*s'\n", sizeof(string), string);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. So I could traverse through my character array to check if the null terminator exists. If not, the array cannot be considered a string.
@MikeHenke Although an array without '\0' cannot be used with library functions that expect null terminated strings, your own functions could still consider it a string of fixed length.
3

You can't. A string with no null terminator is not a string. It's just an array of characters. A C string must have a null terminator to be considered a string.

You'd have to deal with it like you would with an int[] array or any other type of array: keep track of the size separately, if it's not a known fixed size. Since it's not a string, you couldn't call string functions like strlen.

2 Comments

We don't even know what the asker wants to do - "check the validity" of a string? If you don't know what that means, how can you know whether it's possible?
Thank you for your response. I understand the wording of my question - "How to check for validity?" is confusing, but this is the type of response I was looking for (as well as dasblinkenlight's) So because there is no null terminator it is not considered a string.
1

A string needs to end will a null terminator. If you tried to do printf("%s",string) on the second example or use functions like strcmp, strcpy, or strlen it would not work. It is true the a string is just an array of characters with a null terminator, but the null terminator needs to be there if is to be consider a string. So if you are not sure that you actually have an array of characters that is null terminated, you'll need to check for the null terminator.

This distinction is very important, but the similarities can be used to your advantage especially when you get to the embedded level of programming and are reviving characters across a wire. Let's just say you have a buffer that you are putting revived characters in and you are looking for the string "mike". You most likely will not receive a null terminator across a wire so when you search for the string, you'll either need to compare the characters individually or use strncmp which only compares the number of characters that you tell it to which if you have a hardcoded string you can use strlen to get the size of that string you use for strncmp.

1 Comment

I appreciate your answer! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.