私はこのコードを持っています:
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であるため、オペランドのサイズを指定します。aのサイズchar[15]
は常に15になります。
strlen
指定された配列のchar
値を持つ最初の文字列のオフセットであるnullで終了する文字列の長さを示します。呼び出しが有効であるためには、toの引数が実際にnullで終了する文字列を指している必要があります。0
char
strlen
ch
はローカル変数であり、ローカル変数は初期化されません。したがって、それが空の文字列であるというあなたの仮定は正しくありません。そのがらくたでいっぱい。\0
ジャンク文字7文字の後に文字が見つかり、7文字がstrlen
返されたのは偶然の一致でした。
あなたは空の文字列を確保するためにこれらのようなことをすることができます-
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
とstrlen
C++の違い:
1)sizeof
は演算子、strlen
は関数です;
2)の戻り型sizeof
は、であり、ヘッダーのようにsize_t
定義されています(typedef) 。これは、メモリに作成されるこのオブジェクトに対応するために最大化できるメモリ割り当てのバイトサイズを取得します。unsigned int
3)typeをパラメーターとしてsizeof
使用できますが、 charポインター()のみをポインターとして使用でき、''で終了する必要があります。
たとえば、関数をパラメータとして使用することもできます。strlen
char*
\0
sizeof
short f() {return 100;}
std::cout << "sizeof(f()): " << sizeof(f()) << std::endl;
//The result will be sizeof(short), which is 2.
sizeof
4)char配列がパラメーターの場合、それはによって劣化しませんがstrlen
、charポインターとして劣化します。
5)の結果は、コンパイル時間ではなく実行時strlen
に計算され、メモリの実際のサイズではなく、''までの文字列(文字列、文字配列、文字ポインタ)のコンテンツの実際のサイズを取得するために使用されます割り当て。ほとんどのコンパイラは、パラメータが型または変数であるかどうかに関係なく、コンパイル時に結果を計算します。そのため、配列の次元を決定するために使用できます。strlen
\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;
}