C++ 将指针指定给数组

C++ 将指针指定给数组,c++,arrays,pointers,C++,Arrays,Pointers,我试图创建一个生成随机值的数组,然后分配一个指向该数组的指针,以便在其他函数中使用它 问题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;

我试图创建一个生成随机值的数组,然后分配一个指向该数组的指针,以便在其他函数中使用它

问题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]<<" ";
}
int size=100;
int theray[尺寸];
对于(int i=0;i
问题1:这是正确的方法吗

不可以。正确的方法是,如果编译时不知道
大小,则使用
std::vector
,如果编译时不知道大小,则使用
std::array
。这里不需要指针

void foo(const std::vector<int>& v)
{
  // do stuff with v
}

...

std::vector<int> v(size); // vector with size elements
// do something with v

// pass v to a function
foo(v);
1如示例所示,您可以使用
std::vector
作为固定大小的数组,在运行时不需要知道数组的大小。请记住,在构造后可以更改数组的大小


2在您的示例中,
size
不是编译时常量,因此您不能使用
std::array
。但是,如果您将其声明为
const int size=100;
,则它将被视为编译时常量。

您的代码在三个方面有点不正确。首先,不需要使用&theray.array名称内存地址。您只需将指针分配给数组。其次,您正在声明一个包含100个指针的数组。根据您的描述,听起来您只需要一个指针指向数组。您的声明应该是int*parray而不是int*parray[100]。最后,一旦有了指向数组的指针,就可以像访问原始数组一样访问数组的元素,只需使用指针的名称,而不是数组的名称。请尝试更改最后一段代码(从指针声明开始,改为:

int *parray;
parray = theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
    cout<<parray[k]<<" ";
}
int*parray;
帕雷=塞雷;
问题1
这是正确的方法吗

通常不会。这取决于你想要实现什么

对于高级语义,在大多数情况下,您会使用
std::vector
,或者,如果大小是固定的,并且您使用的是C++11,
std::array
。如果您实际上必须转到指针级别,您通常会这样编写:

int *parray = theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
    cout<<parray[k]<<" ";
}
int*parray=theray;
库特
问题1:这是正确的方法吗

。对于大小可以动态变化(在运行时)的数组,请使用
std::vector
。最好避免使用指针和手动内存管理

问题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]<<" ";
}

首先,创建指针的事实,这样你就可以把数组传递给函数。这不是必要的。下面是我如何使用C++标准库中的类来编写程序(C++ 11):< /P>

#包括
#包括
#包括
#包括
//打印向量内容的示例函数
void foo(标准::向量常量和向量)
{
复制(开始(v),结束(v),std::ostream_迭代器(std::cout“”);
}
int main()
{
//填充向量。。。
尺寸_t sz=10;
std::向量v(sz);
生成(开始(v),结束(v),[](){return rand()%100;});
//将其传递给函数。。。
傅(五),;
}

你是指“然后给那个数组分配一个指针,以便在其他函数中使用它”?<代码> int int [sie](<代码)>这不是有效的C++;<代码>大小<代码>需要声明为“代码> const <代码>,以便工作。@ Dukele:答案表示“编译时间”,因为它意味着“编译时间”。在C++中,可变长度数组的唯一方法是<代码> STD::vector < /C> >我可能对代码< > STD::数组< /C> >有点陌生,但是在快速的谷歌搜索之后,我看到了现在。BTW,<代码> int St= 100;int [Sea[St]; >不能在一些编译器中通过。
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

// Sample function that prints the vectors's content
void foo(std::vector<int> const& v)
{
    copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    // Populate the vector...
    size_t sz = 10;
    std::vector<int> v(sz);
    generate(begin(v), end(v), [] () { return rand() % 100; });

    // Pass it to a function...
    foo(v);
}