C++ 我似乎无法将指针分配给数组,然后更改数组的内容
我不知道如何使函数返回数组,因此我决定尝试将一个空数组(大小正确)传递到函数中,然后将地址重新分配给相同大小的不同数组。这是一种做事的方式吗???有人能告诉我怎么做吗?如果这是错误的,你能告诉我怎么做吗 这是我的密码:C++ 我似乎无法将指针分配给数组,然后更改数组的内容,c++,arrays,pointers,C++,Arrays,Pointers,我不知道如何使函数返回数组,因此我决定尝试将一个空数组(大小正确)传递到函数中,然后将地址重新分配给相同大小的不同数组。这是一种做事的方式吗???有人能告诉我怎么做吗?如果这是错误的,你能告诉我怎么做吗 这是我的密码: #include <iostream> #include <cmath> using namespace std; void ArrayFiller(int earray,int s, int f){ int *ptrarray = &
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray,int s, int f){
int *ptrarray = &earray;
int prenum_size = std::abs(s) + f - 1;
int pre_num[prenum_size];
for(int x=s;x<f;x++){
pre_num[x+std::abs(s)] = x;
}
*ptrarray = pre_num;
}
int Main(){
int first = -10;
int second = 15;
int temp[abs(first) + abs(second)];
ArrayFiller(temp, first, second);
int n = sizeof(temp)/sizeof(temp[0]);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
return 0;
}
#包括
#包括
使用名称空间std;
无效阵列填充器(内部阵列、内部s、内部f){
int*ptrarray=&array;
int prenum_size=std::abs(s)+f-1;
int prenum[婚前大小];
对于(int x=s;x我认为您正在寻找类似的内容:
#include <iostream>
#include <cmath>
using namespace std;
void ArrayFiller(int earray[],int s, int f){
for(int x=s;x<f;x++){
earray[x+(std::abs(s))] = x;
}
}
int main(){
int first = -10;
int second = 15;
int n = abs(first)+abs(second);
int* temp = new int[n];
ArrayFiller(temp, first, second);
for (int i = 0; i < n; i++) {
cout << temp[i] << ' ';
}
delete [] temp;
return 0;
}
#包括
#包括
使用名称空间std;
无效阵列填充器(内部阵列[],内部阵列s,内部阵列f){
对于(int x=s;x)代码未按原样编译:ArrayFiller
的第一个参数是int
,但您给它一个数组。@EtienDemartel如何将第一个参数设置为数组。我会使用int-array[]您可以,尽管以这种方式声明的数组参数实际上是指针,但是您可以将其声明为“代码> int *EARLY < /Cord>”,甚至不必在该函数中声明“代码> PrTrase<代码>。<代码>主< /代码>应该是“代码>主< /代码>?BTW VLA在C++中是不合法的,只使用<代码> STD::vector < /代码>