• ベストアンサー

char型配列について

基本的なことですが、 char str[5]="Hello"; --> str[0]='H' str[1]='e' str[2]='l' str[3]='l' str[4]='o' str[5]='\0' では、ないのでしょうか? エラーが出ます。 //error C2117: 'str' : 指定された配列には、初期化子が多すぎます。 char str[6]="Hello"; では、コンパイルできます。 ---------------------------------- また、 #include<iostream> using namespace std; int main() { char str[6]="Hello"; cout << str << endl; for(int i=0;i<7;i++) { cout << "i=" << str[i]; if(str[i]=='\0'){cout << " NULL" << endl;} else{cout << endl;} } getchar();return 0; } ----------------------------------------------- とすると、 Hello i=H i=e i=l i=l i=o i= NULL <--ここで、NULLなら、 i=フ <--このぶんは、いらないと思うのですが、、、 となります。 str[6] i=6 は、何を意味するのでしょうか? Visual C++ NET を使用しています。 よろしくお願いします。

noname#6117
noname#6117

質問者が選んだベストアンサー

  • ベストアンサー
  • kary
  • ベストアンサー率55% (10/18)
回答No.3

char str[5]では5個のcharが確保されます。"Hello"ではH,e,l,l,oの5個と、最後の'\0'を含めて6個のcharが必要ですので、char str[5]="Hello"ではエラーとなります。また、i=6の場合の出力ですが、char str[6]では0番目から5番目までの6個のcharが確保されますので、6番目はありません。'フ'というのはゴミが出力されたのだと思います。

noname#6117
質問者

お礼

回答ありがとうございます。 >char str[5]は5つの領域をとるという意味 >よくわかりました。 >もう一度入門書を読み直します。 >いつの頃からか勘違いをしていました。

その他の回答 (2)

  • asuca
  • ベストアンサー率47% (11786/24626)
回答No.2

>str[6] i=6 は、何を意味するのでしょうか? char str[5]は5つの領域をとるという意味ですのでstr[0」からstr[4]までの領域しか確保しません。 ですからstr[5」に入れたかったら0から5までの6つの領域を確保するためにchar str[6]にする必要があるかと思います。 char str[6]="Hello"; だったらstr[0]には何も定義されていないことになります。

noname#6117
質問者

お礼

回答ありがとうございます。 >char str[5]は5つの領域をとるという意味 >よくわかりました。 >もう一度入門書を読み直します。 >いつの頃からか勘違いをしていました。

回答No.1

>for(int i=0;i<7;i++) これは、 iが7になるまで、すなわち0~6の間繰り返されます。 >char str[6]="Hello" これは要素数が6(0~5)の配列の宣言です。

noname#6117
質問者

お礼

回答ありがとうございます。 >char str[5]は5つの領域をとるという意味 よくわかりました。 もう一度入門書を読み直します。 いつの頃からか勘違いをしていました。

関連するQ&A

  • char型変数のアドレスを coutで表示するには

    #include <iostream> using namespace std; int main() { bool b; int i; short s; long l; float f; double d; char c; //上で宣言した変数のアドレスを表示 cout << "bool &b " << &b << endl; cout << "int &i " << &i << endl; cout << "short &s " << &s << endl; cout << "long &l " << &l << endl; cout << "float &f " << &f << endl; cout << "double &d " << &d << endl; cout << "char &c " << &c << endl; //「char &c 」とのみ表示される cout << '\n'; //char型のみ printf で再表示 printf("char &c %p\n", &c); //「char &c ********」と表示される return 0; } 上のプログラムを実行すると cout << "char &c " << &c << endl; のところだけ、アドレスが表示されません。 printfを使えば、char型の変数のアドレスも表示されるのですが…。 coutを使ってchar型のアドレスを表示させるにはどうすればいいのでしょうか。 よろしくお願いします。

  • 下記、プログラム内の「char *」の役割

    C++初心者です。 縦長になってしまいますが、 『 #include <iostream.h> void show(int); void show(double); void show(char *);   ←左記の記述の使い方 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char *z) { cout << z << endl; } 』 のプログラムにおいて、「char *」の使い方がいまいち理解できません。 上記プログラムですとエラーが表示されないのですが、下記のプログラムだとエラーが発生します。 『 #include <iostream.h> void show(int); void show(double); void show(char);   //←---------上記と違う行 int main(void) { show(1); show(0.25); show("文字列"); return 0; } void show(int x) { cout << x << endl; } void show(double y) { cout << y << endl; } void show(char z) {  //←---------上記と違う行 cout << z << endl; } 』 なぜ、ポインタ(*)を付けないといけないのか分かりやすく教えていただけましょうか。

  • 配列の練習問題

    #include<iostream> using namespace std; //count関数の宣言 int count(char str[], char ch); int main() { char str[100]; char ch; cout << "文字列を入力して下さい。\n"; cin >> str; cout << "文字列から探す文字を入力して下さい。\n"; cin >> ch; int c = count(str, ch); cout << str << "の中に" << ch << "は" << c << "個あります。\n"; return 0; } //count関数の定義 int count(char str[], char ch) { int i = 0; int c = 0; while (str[i]) { if (str[i] == ch) c++; i++; } return c; } こんにちは。 この問題の解答のプログラムの意味がイマイチ解らないので良かったら教えて下さい。 確認がてらに質問します。 よろしくお願いします。

  • string から unsigned char へ

    unsigned char* uchar_string(string* str) { int length = str->length(); const char* cchar = str->c_str(); unsigned char* uchar = new unsigned char[length+1]; for(int i=0; i=length; i++) { uchar[i] = (unsigned char)cchar[i]; } return uchar; } int main() { string str; cin >> str; unsigned char* uc; uc = uchar_string(&str); cout << uc; delete uctest; return 0; } このようにしたところ、cout << uc; が出力されず入力待ちとなり正常に動きませんでした。 原因がわからないです。原因と解決方法のご教授願います。

  • int型変数をchar型変数に格納する方法

    int型数値をchar型変数に格納したいのですが、 sprintf()とatoi()を用いてやりましたが、 うまくいきません。 考えたコードを下記します。 int get_y(){ int y=2005; return y; } int main(){ char year; char str[5]; //intをcharに変換して格納している sprintf(str,"%d",get_y()); //数値として代入 year=atoi(str); //yearには2005が格納されていない! cout<<year<<endl; char ans; //このchar型変数に数値を代入する。 ans=year; cout<<ans<<endl; return 0; } お手数をお掛けしますが、 よろしくお願い致します。

  • 配列とポインタでの書き直しその2

    配列とポインタでの書き直し(c++)その1 のつづき cout << endl; cout << setw(5) << "Sum" << setw(12) << "Number" << setw(14) << "Probability" << setw(10) << "Error" << endl; cout << "-----------------------------------------" << endl << endl; cout << setiosflags(ios::fixed | ios::showpoint); for (int k = 0; k <= 10; k++) { probability = static_cast<double>(sum[k])/throws; cout << setw(4) << k+2 << setw(12) << sum[k] << setprecision(6) << setw(14) << probability << setprecision(2) << setw(11) << getError(probability, k, error) << endl; } return 0; } // Roll two dice. int rollDice() { int dice1, dice2; dice1 = rand()%6 +1; dice2 = rand()%6 +1; return (dice1 + dice2); } // Example: for error dice 12 // // prob = sum[10] / throws, // // // |prob - e[10]| // error = ----------------- x 100 // e[10] // double getError(double p, int i, double e[]) { return ((fabs(p - e[i])/e[i])*100); } 関連URL: http://www.okweb.ne.jp/kotaeru.php3?q=243537

  • プログラムの動作

    10文字をスキップするプログラムなのですが、どのようにスキップしているのかわからないので教えてください。 下にソースコードを書きます。 #include <iostream> using namespace std; //10文字をスキップする istream &skipchar(istream &stream) { int i; char c; for(i=0; i<10; i++)stream >> c; return stream; } int main() { char str[80]; cout << "いくつかの文字を入力する:"; cin >> skipchar >> str; cout << str << endl; return 0; } よろしくおねがいします。

  • 文字列クラスを作りたいと思っています

    文字列クラスを作りたいと思っています。 以下のようなところまでは作れましたが、 エラーがでてしまいます。 どこかおかしいところがあるのでしょうか? *********************************************** #include<stdio.h> #include<string.h> class stt { public: char *str; int len; bool maked; stt::stt() { len=0; str=NULL; maked=false; } stt::~stt() { delete[] str; } stt &operator =(char *c) { if(maked) { delete[] str; maked=false; } len=strlen(c); str=new char[len]; strcpy(str,c); maked=true; return (*this); } virtual operator char*() { return str; } }; int main() { stt s; s="Hello World"; printf(s); getchar(); return 0; } ***********************************************

  • 配列のコピー

    配列bufの内容をstrにコピーしてgetsを使い 表示させたいのですが、うまくいかず 余計な文字まで出力されます、どのようにすれば うまくいくでしょうか? どなたかアドバイスよろしくお願いしますm(_ _)m #include <stdio.h> void main(){ char buf[256]="message"; char str[256]; int i = 0; while(buf[i] != NULL){ str[i] = buf[i]; i++; } printf(str); }

  • wstringの主力

    wstring (または wchar_t)の出力がうまくいかず困っています。 基本的な質問になるかと思いますがよろしくお願いします。 #include <string> #include <wchar.h> using namespace std; int main() {  wstring str = L"test";  wprintf(L"%s \n", str.c_str());//(1)  wprintf(L"%s \n", L"test");//(2)  wprintf(L"test \n");//(3)  // cout << str <<endl; //(4)  cout << str.c_str() <<endl;//(5) } 出力結果 (1) t (2) t (3) test (4) コンパイルエラー (5) 望むのは当然(3)の出力です。 web上で(1)や(4)のような記述はみかけたのですが、(4)に関してはなぜコンパイルエラーになるかもわからず。。 os linux. gcc 4.1.1

専門家に質問してみよう