我有这个代码:
int main()
{
char ch[15];
cout<<strlen(ch)<<endl; //7
cout<<sizeof(ch)<<endl; //15
return 0;
}
为什么strlen(ch)
即使它是空char
数组也会给出不同的结果?
您的代码具有未定义的行为,因为您正在使用 读取数组的未初始化值strlen
。如果你想要一个确定的结果,strlen
你必须初始化(或分配给)你的数组。
例如
char ch[15] = "Hello, world!";
或者
char ch[15] = {};
sizeof
将给出其操作数的大小,因为char
根据定义,a 的大小为 1,a 的大小char[15]
将始终为 15。
strlen
给出一个空终止字符串的长度,它是给定数组中第一个char
带有值的字符串的偏移量. 要使调用有效,参数 to 必须实际指向一个以空字符结尾的字符串。0
char
strlen
ch
是一个局部变量,局部变量没有被初始化。因此,您认为它是空字符串的假设是不正确的。它充满了垃圾。\0
巧合的是,在 7 个垃圾字符之后发现了一个字符,因此strlen
返回了 7 个。
你可以做这样的事情来确保一个空字符串 -
char ch[15]={0};
ch[0]='\0`;
strcpy(ch,"");
这是一个类似的线程以供更多阅读
问题出在
strlen(ch);
strlen
计算字符数,直到击中\0
符号。在这里,ch
是未初始化的,所以strlen
可以返回任何东西。
至于 的结果strlen
,在您的情况下,您有一个未初始化的char
数组,因此strlen
只发生了7:数组元素 8 处必须有一个空字符,但此代码strlen
每次都可能给出不同的结果。
始终初始化字符串,使用数组很容易:char str[15] = {0};
sizeof
是一个运算符,用于获取变量或数据类型的大小,或者数组占用的字节数,而不是C 字符串的长度;不要期望strlen
和strcpy
可以互换,甚至可以以任何有用的方式进行比较。
例如:
int main()
{
char str[15] = "only 13 chars";
cout << "strlen: " << strlen(str) << endl;
cout << "sizeof: " << sizeof(str) << endl;
}
输出是:
strlen: 13
sizeof: 15
返回 str 的长度。
C 字符串的长度由终止空字符确定:AC 字符串与字符串开头和终止空字符之间的字符数一样长。
sizeof
返回字节数 (15)。您的数组被垃圾填充,因此 strlen 可以返回任何数字。正确的例子是
int main()
{
char ch[15] = {0};
cout<<strlen(ch)<<endl; //0
cout<<sizeof(ch)<<endl; //15
return 0;
}
sizeof
C++和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;
}