char ** Ptr;
char apple[15];
char cake[15];
Ptr = new char*[2];
Ptr[0]=apple;
Ptr[1]=cake;
更新Ptr[1]
后,不幸的是新的元素,Ptr[0]
成为除了Ptr[1]
cake
。我很确定问题在于我如何声明Ptr
我基本上希望它是一个字符串数组。有没有办法做到这一点,我保持char ** Ptr
?使用char **其中覆盖以前的元素
编辑:
{
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
(woops),因为每个人都非常善意指出。任何帮助将不胜感激,但。
无论如何,谢谢你的时间。
[Works fine here](http://ideone.com/nKTsd7)。 –
你能告诉我们你是如何解除引用'Ptr'来查看它的内容吗? –
问题不在这里明显 –