1

I am writing a function to check length of a char array in c. It takes another parameter, which sets the limit for \0 check.

int string_length(char* string, int maximum_length) {

    int i = 0;

    while(string[i] != '\0' && i < maximum_length) {
        i++;
    }

    return i;

}

In this answer, it is mentioned that a char array must be null terminated, if it is created using {} syntax. I call the above function with not terminated char array & the result is 10(9 letters + 1).

char not_terminated_string[] = {'m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g' };
int length = string_length(not_terminated_string, 100);
// length is 10

I am not able to understand why is it this way.

explorer
  • 944
  • 8
  • 18
  • 2
    Reading outside of the bounds of an array is *undefined behavior* - so if you pass an array that is not null-terminated *and* give a wrong array size, your function breaks – UnholySheep Jul 11 '20 at 22:38
  • I don't understand the question. There is no terminator for 9 characters in the array and the function stops at the limit 10. There happens to be one found. – Weather Vane Jul 11 '20 at 22:43
  • 1
    @WeatherVane I agree with your comment *"So the function finds a `0`. Why shouldn't it?"* The fact that it was found it at index 10 has nothing to do with `maximum_length`. That's all I was saying. – user3386109 Jul 11 '20 at 22:49
  • 1
    Your edits break the question and the answer. With the original definition, the size of the array was counted from the initial values given, so it was nine characters, and the tenth was not controlled. With the new definition, the `[15]` sets the array size to 15 elements. Nine are set from the characters given, and the rest are filled in with zeros. Then `string_length` produces nine, as your code says in a comment. But your text above that says 10. Meanwhile, the answers written for the old question are wrong for the new question. That is a violation of Stack Overflow protocol. – Eric Postpischil Jul 11 '20 at 23:42
  • @EricPostpischil, apologies for that. I will revert my updates to the question. – explorer Jul 11 '20 at 23:59

2 Answers2

3

For the following line, C compiler creates an array of 10 char size elements and lays down the first 9 characters, adding a string \0 delimiter at the very end.

char *a = "my string";

Considering the following line; C compiler creates an array of 9 char size elements and lays down the characters. A string delimiter is not added at the very end. If there happens to be a zero value at the 10th byte (byte number 9), that would be only by chance.

char b[] = { 'm', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g' };

The statement "a char array must be null terminated, if it is created using {}" means, if you want to be able to use that char array as a string (like, to be able to use it in a printf), then you should add a string terminating character by yourself, like;

char b[] = { 'm', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0' };
ssd
  • 2,340
  • 5
  • 19
  • 37
1

Your program produced ten because you defined an array of only nine non-null characters, but it happened to be followed by one more non-null character and then a null character. (Technically, the behavior of your program is not defined by the C standard due to overrunning the array, and there are other ways your program could then have misbehaved to produce ten, but this is the most likely occurrence.)

The declaration char not_terminated_string[] = {'m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g' }; defines not_terminated_string to be an array of nine char, which are initialized with the given characters. No null character is automatically appended to this array.

When your program passed this array to string_length, that routine counted the nine characters in the array, and then it attempted to look at the tenth. It appears, most likely, that the next byte in memory was not null, so the routine counted it and looked at the eleventh. It appears that one was null, so the routine stopped and returned ten.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Oh, okay! It simply **happens** to be. Thanks for your reply. – explorer Jul 11 '20 at 23:05
  • Even though the question is already answered, I saw that I did not provided more details about the question. So, I have updated the same. If there are any further comments on the update, I would really appreciate them. Good day! – explorer Jul 11 '20 at 23:20