0

我有这个代码:

int main()
{
    char ch[15];
    cout<<strlen(ch)<<endl; //7
    cout<<sizeof(ch)<<endl; //15
    return 0;
}

为什么strlen(ch)即使它是空char数组也会给出不同的结果?

4

6 回答 6

6

您的代码具有未定义的行为,因为您正在使用 读取数组的未初始化值strlen。如果你想要一个确定的结果,strlen你必须初始化(或分配给)你的数组。

例如

char ch[15] = "Hello, world!";

或者

char ch[15] = {};

sizeof将给出其操作数的大小,因为char根据定义,a 的大小为 1,a 的大小char[15]将始终为 15。

strlen给出一个空终止字符串的长度,它是给定数组中第一个char带有值的字符串的偏移量. 要使调用有效,参数 to 必须实际指向一个以空字符结尾的字符串。0charstrlen

于 2012-08-13T08:13:46.570 回答
5

ch是一个局部变量,局部变量没有被初始化。因此,您认为它是空字符串的假设是不正确的。它充满了垃圾。\0巧合的是,在 7 个垃圾字符之后发现了一个字符,因此strlen返回了 7 个。

你可以做这样的事情来确保一个空字符串 -

char ch[15]={0};
ch[0]='\0`;
strcpy(ch,"");

这是一个类似的线程以供更多阅读

C++ 中的变量初始化

于 2012-08-13T08:12:59.040 回答
2

问题出在

strlen(ch);

strlen计算字符数,直到击中\0符号。在这里,ch是未初始化的,所以strlen可以返回任何东西

于 2012-08-13T08:13:05.403 回答
1

至于 的结果strlen,在您的情况下,您有一个未初始化的char数组,因此strlen发生7:数组元素 8 处必须有一个空字符,但此代码strlen每次都可能给出不同的结果。

始终初始化字符串,使用数组很容易:char str[15] = {0};

sizeof是一个运算符,用于获取变量或数据类型的大小,或者数组占用的字节数,而不是C 字符串的长度;不要期望strlenstrcpy可以互换,甚至可以以任何有用的方式进行比较。

例如:

int main()
{
    char str[15] = "only 13 chars";

    cout << "strlen: " << strlen(str) << endl;
    cout << "sizeof: " << sizeof(str) << endl;
}

输出是:

strlen: 13
sizeof: 15
于 2012-08-13T08:13:08.993 回答
0

返回 str 的长度。

C 字符串的长度由终止空字符确定:AC 字符串与字符串开头和终止空字符之间的字符数一样长。

sizeof返回字节数 (15)。您的数组被垃圾填充,因此 strlen 可以返回任何数字。正确的例子是

int main()
{
    char ch[15] = {0};
    cout<<strlen(ch)<<endl; //0
    cout<<sizeof(ch)<<endl; //15
    return 0;
}
于 2012-08-13T08:14:21.660 回答
0

sizeofC++和C++的区别strlen

1)sizeof运算符strlen函数

2) 的返回类型sizeof是,并在其头部size_t定义(typedef) ;它获取内存分配的字节大小,该字节大小可以最大化以容纳要在内存中创建的对象unsigned int

3)sizeof可以使用type作为参数,而strlen只能使用char指针( char*)作为指针,并且必须以' \0'结尾; sizeof也可以使用函数作为参数,例如:

short f() {return 100;} 
std::cout << "sizeof(f()): " << sizeof(f()) << std::endl;
//The result will be sizeof(short), which is 2.

4)如果char数组是参数,则不会降级sizeof,而strlen会降级为char指针;

5) 的结果strlen将在运行时计算,而不是编译时strlen用于获取字符串内容(字符串、char数组、char指针)直到' \0'的真实大小,而不是内存的真实大小分配。大多数编译器都会在编译时计算结果sizeof无论参数是类型还是变量,这就是为什么sizeof(x)可以用来决定数组的维数:

char str[20]="0123456789"; 
int a=strlen(str); //a=10; 
int b=sizeof(str); //while b=20; 

7)如果参数sizeof类型,则括号是强制性的,而如果参数是变量,括号是可选的,因为sizeof是运算符而不是函数

8) 当你使用结构化类型变​​量作为参数时,sizeof会返回它的真实大小,当你使用静态数组时,sizeof会返回数组大小。但是运算符不能返回动态外部sizeof创建的数组的大小。因为 sizeof 是编译时运算符

以下是 sizeof 和 strlen 的示例:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

short f1 ()
{
  return 100;
}

int f2 ()
{
  return 1000;
}

int main()
{
  char* char_star = "0123456789";
  // char_star is a char pointer, sizeof will return the pointer size allocated in memory: depends on your machine
  std::cout << "sizeof(char_star):" << sizeof(char_star) << std::endl;
  // *char_star is the first element of the string, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_star):" << sizeof(*char_star) << std::endl;
  // char_star is a char pointer, strlen will return the real size of the string until '\0': 10
  std::cout << "strlen(char_star):" << strlen(char_star) << std::endl;
  std::cout << std::endl;

  char char_array[] = "0123456789";
  // char_array is a char array, sizeof will return the array size allocated in memory, with a '\0' at the end: 10 + 1
  std::cout << "sizeof(char_array):" << sizeof(char_array) << std::endl;
  // *char_array is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array):" << sizeof(*char_array) << std::endl;
  // char_array is a char array, strlen will return the real size of the string until '\0': 10
  std::cout << "strlen(char_array):" << strlen(char_array) << std::endl;
  std::cout << std::endl;

  char_array_fixed[100] = "0123456789";
  // char_array_fixed is a char array with fixed size, sizeof will return the array size allocated in memory: 100
  std::cout << "sizeof(char_array_fixed):" << sizeof(char_array_fixed) << std::endl;
  // *char_array_fixed is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array_fixed):" << sizeof(*char_array_fixed) << std::endl;
  // *char_array_fixed is a char array with fixed size, strlen will return the real content size of the string until '\0': 10
  std::cout << "strlen(char_array_fixed):" << strlen(char_array_fixed) << std::endl;
  std::cout << std::endl;

  int int_array[100] = {0,1,2,3,4,5,6,7,8,9};
  // int_array is a int array with fixed size, sizeof will return the array size allocated in memory: 100
  std::cout << "sizeof(int_array):" << sizeof(int_array) << std::endl;
  // *int_array is the first element of the array, it is an int, sizeof will return the int size allocated in memory: depends on your machine, normally is 4
  std::cout << "sizeof(*int_array):" << sizeof(*int_array) << std::endl;
  // int_array is a int array with fixed size, strlen will throw exception 
  //std::cout << "strlen(int_array):" << strlen(int_array) << std::endl;
  std::cout << std::endl;

  char char_array2[] = {'a', 'b', '3'};
  // char_array2 is a char array, sizeof will return the array size allocated in memory: 3
  std::cout << "sizeof(char_array2):" << sizeof(char_array2) << std::endl;
  // *char_array2 is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array2):" << sizeof(*char_array2) << std::endl;
  // *char_array2 is a char array, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(char_array2):" << strlen(char_array2) << std::endl;
  std::cout << std::endl;

  char char_array3[] = {"abc"};
  // char_array3 is a char array, sizeof will return the array size allocated in memory, with a '\0' at the end : 3 + 1
  std::cout << "sizeof(char_array3):" << sizeof(char_array3) << std::endl;
  // *char_array3 is the first element of the array, it is a char, sizeof will return the char size allocated in memory: depends on your machine, normally is 1
  std::cout << "sizeof(*char_array3):" << sizeof(*char_array3) << std::endl;
  // *char_array3 is a char array, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(char_array3):" << strlen(char_array3) << std::endl;
  std::cout << std::endl;

  std::string str = {'a', 'b', '3', '\0', 'X'};
  // str is a string, sizeof will return the string size allocated in memory (string is a wrapper, can be considered as a special structure with a pointer to the real content): depends on your machine, normally is 32
  std::cout << "str:" << str << std::endl;
  std::cout << "sizeof(str):" << sizeof(str) << std::endl;
  // *str means nothing, sizeof will throw exeption
  //std::cout << "sizeof(*str):" << sizeof(*str) << std::endl;
  // str is a string, strlen will return the real content size of the string until '\0': 3
  std::cout << "strlen(str):" << strlen(str.c_str()) << std::endl;
  std::cout << std::endl;

  // sizeof is an operation, if the parameter is a type, parentheses are mandatory
  std::cout << "sizof(int):" << sizeof(int) << std::endl;
  // sizeof is an operation, if the parameter is a variable, parentheses are optional
  std::cout << "sizof char_star:" << sizeof char_star << std::endl;
  std::cout << "sizof char_array:" << sizeof char_array << std::endl;
  // sizeof is an operation, can take a function as parameter
  std::cout << "sizeof(f()): " << sizeof(f1()) << std::endl;
  std::cout << "sizeof(f()): " << sizeof(f2()) << std::endl;
}
于 2018-06-01T12:29:33.890 回答