简体   繁体   中英

What is the length of string when we don't explicitly initialize a character array with null character?

I tried the following code on GNU GCC compiler and its giving me output as 26. But I don't understand how the code is working especially what actually strlen() function is doing.

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

int main()
{
    char b[]={'G','E','E','K','S',' ','F','O','R',' ','F','U','N'};
    cout<<sizeof(b)<<endl;
    cout<<strlen(b)<<endl;
    return 0;
}

What is the length of string when we don't explicitly initialize a character array with null character?

The length of the string is the number of characters it contains. (Let us ignore for simplicity the fact that there is a difference between the number of graphemes, code points and code units, all of which are the length of the string depending on perspective. In the context of this answer, character == code unit).

The length of a null terminated string is the number of characters before the null terminator. If a string doesn't contain a null terminator, then it isn't a null terminated string.

 strlen(b)

b isn't a null terminated string. strlen requires that the argument points to a null terminated string. If the requirement isn't satisfied, the behaviour of the program is undefined.

But its working except that for giving o/p 13, its giving output 26

The behaviour is undefined. Possible behaviours include, none of which are guaranteed:

 - working
 - not working
 - random output
 - non-random output
 - the expected output
 - unexpected output
 - no output
 - any output
 - crashing at random
 - crashing always
 - not crashing
 - corruption of data
 - different behaviour, when executed on another system
 -                    , when compiled with another compiler
 -                    , on tuesday
 -                    , only when you're not looking
 - same behaviour in all of the above cases
 - anything else within the power of the computer (hopefully limited by the OS)

Undefined behaviour is undesirable in any program.

But I don't understand how the code is working especially what actually strlen() function is doing.

When you pass a string to strlen that is not null terminated, you are invoking undefined behavior. Don't count on any predictable behavior.

The Length of String or strlen refers to a C string that is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself). for example:

char TestStr[25] = "Test String";

if you use sizeof(TestStr) it will evaluate the 25 chars in the array but with strlen(TestStr) will evaluate only 11.

Hope this helps

Another case related to your issue:

int main()
{
  static const char fun_text[] = {'f', 'u', 'n', '\0', 'c', 'a', 's', 'e'};
  std::cout << strlen(fun_text) << std::endl;
  std::cout << sizeof(fun_text) << std::endl;
  return 0;
}

In the above case, the array size is 8.
However, the strlen function will return 3 because it runs into a '\\0' character before reaching the end of the array.

Summary

The strlen function returns length based on content.
The sizeof operator return the length of the array, regardless of content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM