[英]C++ strlen(ch) and sizeof(ch) strlen
我有以下代碼:
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
的大小按定義是1, char[15]
的大小將始終為15。
strlen
給出了一個空終止字符串的長度,該長度是給定char
數組中第一個char
的偏移量,值為0
。 為了使對strlen
的調用有效,to的參數實際上必須指向以null終止的字符串。
ch
是局部變量,並且局部變量未初始化。 因此,您認為它是空字符串的假設是不正確的。 它充滿了垃圾。 碰巧的是,在7個垃圾字符后找到\\0
字符,因此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;
}
C ++中的sizeof
和strlen
之間的區別:
1) sizeof
是一個運算符 , strlen
是一個函數 ;
2) sizeof
的返回類型為size_t
,並且在其標頭中將其定義為(typedef) unsigned int
; 它獲得內存分配的字節大小,該字節大小可以最大化以容納要在內存中創建的對象 ;
3) sizeof
可以使用type作為參數,而strlen
只能使用char指針( char*
)作為指針,並且必須以' \\0
'結尾; sizeof
也可以使用function作為參數,例如:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.