我想创建一个数组,产生随机值,然后分配一个指向该数组的指针,以便在其他函数中使用它。指定一个数组的指针
问题1:这是正确的做法吗?
问题2:当我运行下面的代码时,我的指针函数生成的值与实际数组的值不一致。我究竟做错了什么?
int size = 100;
int theray[size];
for(int i=0; i<size; i++)
{
theray[i] = (rand()%100);
}
//Output array
cout<<"The array: ";
for(int j=0; j<size; j++)
{
cout<<theray[j]<<" ";
}
cout<<endl;
int (*parray)[100] = &theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
cout<<*parray[k]<<" ";
}
通过“什么你的意思,然后才能在其他使用它的指针分配给数组功能”?这味道不好 – 2013-03-06 07:21:32
指针是[** EVIL **](http://www.parashift.com/c++-faq/defn-evil.html)! – 2013-03-06 07:26:33
'int theray [size];'这是无效的C++; 'size'需要被声明为'const'才能工作。 – 2013-03-06 07:28:33