char ** Ptr;
char apple[15];
char cake[15];
Ptr = new char*[2];
Ptr[0]=apple;
Ptr[1]=cake;
残念ながら更新後Ptr[1]
、おまけになりPtr[0]
ます。問題は、本質的に文字列の配列にしたいと宣言した方法にあると確信しています。私が保管している場所でこれを行う方法はありますか?cake
Ptr[1]
Ptr
char ** Ptr
編集:
{
char **Ptr;
{
char apple[15];
Ptr = new char*[2];
for(int k=0;k<2;k++)
{
memset(apple,0,15);
//apple=
Ptr[k]=apple; //Note that apple in fact changes everytime
}
//Originally I had Ptr[k]=apple but it seemed I was merely copying the address of
//apple which works great except when I leave the scope trying to call it the addr no
//longer exists and I was getting lucky the last entry showed up at all. So I then
//figured I would use
strcpy(Ptr[k],apple);
//I then checked the value for both was correct even when I deleted apple.
// Finally I leave the scope where all this is taking place
}
cout<<Ptr[0];
cout<<Ptr[1];
}
幸いなことに、それらは実際には同等のゴミでした。最初の数文字は同じでしたが、ほとんどがゴミでした。おそらくスコープの問題だと思ったPtr
ので、基本的にはグローバルな同じ問題にしました。とにかく、別の変数を作成したので、誰もが親切に指摘してくれたので、問題は含まれていませんでしたが、元の問題を残しましたcake
(おっと)。ただし、どんな助けでも大歓迎です。
とにかく、お時間をいただきありがとうございます。