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] = {};

sizeofcharのサイズは定義上1であるため、オペランドのサイズを指定します。aのサイズchar[15]は常に15になります。

strlen指定された配列のchar値を持つ最初の文字列のオフセットであるnullで終了する文字列の長さを示します。呼び出しが有効であるためには、toの引数が実際にnullで終了する文字列を指している必要があります。0charstrlen

于 2012-08-13T08:13:46.570 に答える
5

chはローカル変数であり、ローカル変数は初期化されません。したがって、それが空の文字列であるというあなたの仮定は正しくありません。そのがらくたでいっぱい。\0ジャンク文字7文字の後に文字が見つかり、7文字がstrlen返されたのは偶然の一致でした。

あなたは空の文字列を確保するためにこれらのようなことをすることができます-

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};

sizeofC文字列の長さではなく、変数またはデータ型のサイズ、または配列が占めるバイト数を取得するために使用される演算子です。互換性があること、または有用な方法で比較できることを期待strlenしないでください。strcpy

例えば:

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

sizeofstrlenC++の違い:

1)sizeof演算子strlen関数です;

2)の戻り型sizeofは、であり、ヘッダーのようにsize_t定義されています(typedef) 。これは、メモリに作成されるこのオブジェクトに対応するために最大化できるメモリ割り当てのバイトサイズを取得しますunsigned int

3)typeをパラメーターとしてsizeof使用できますが、 charポインター()のみをポインターとして使用でき、''で終了する必要があります。 たとえば、関数をパラメータとして使用することもできます。strlenchar*\0sizeof

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

sizeof4)char配列がパラメーターの場合、それはによって劣化しませんがstrlen、charポインターとして劣化します。

5)の結果は、コンパイル時間ではなく実行時strlenに計算され、メモリの実際のサイズではなく、''までの文字列(文字列、文字配列、文字ポインタ)のコンテンツの実際のサイズを取得するために使用されます割り当て。ほとんどのコンパイラは、パラメータが型または変数であるかどうかに関係なく、コンパイル時に結果を計算します。そのため、配列の次元を決定するために使用できます。strlen\0sizeofsizeof(x)

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

7)のパラメータがsizeofタイプ場合、括弧は必須ですが、パラメータが変数の場合、は関数ではなく演算子であるため、括弧はオプションですsizeof

8)構造化または変数をパラメーターとして使用する場合は、実際のサイズsizeofを返します。静的配列を使用する場合は、配列サイズを返します。ただし、演​​算子は、動的または外部で作成された配列のサイズを返すことはできません。sizeofはコンパイル時の演算子であるため。 sizeofsizeof

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 に答える