C++ C++;通过引用传递来更改函数内数组的值
问题是我的指针总是指向同一个内存地址,这使得它的行为与我想要的不一样 我想在一个函数期间将几个值存储在一个数组中,但也必须将它们写入main中存在的数组中,方法是将它们作为引用传递,这样我就不必返回。有人能帮我出什么事了吗 这里的代码有效,你会看到我得到了相同的地址C++ C++;通过引用传递来更改函数内数组的值,c++,C++,问题是我的指针总是指向同一个内存地址,这使得它的行为与我想要的不一样 我想在一个函数期间将几个值存储在一个数组中,但也必须将它们写入main中存在的数组中,方法是将它们作为引用传递,这样我就不必返回。有人能帮我出什么事了吗 这里的代码有效,你会看到我得到了相同的地址 #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; void creatingar
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
void creatingarray(int starttemp2, int *arrayc2[], int nlength2, int step2){
int *newtemp = new int;
*newtemp = starttemp2;
//cout << phead;
cout << *newtemp << " " << endl;
for (int i=0; i < nlength2; i++){
arrayc2[i] = newtemp;
*newtemp = *newtemp + step2;
cout << *arrayc2[i] << " " << arrayc2[i] <<" ";
cout << *newtemp << " "<<endl;
}
for (int i=0;i<nlength2;i++)
cout<< *arrayc2[i] << " ";
}
int main()
{
int step;
int starttemp;
int endtemp;
cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step";
cout << "step:";
cin >> step ;
cout << "begintemperatuur in celsius: ";
cin >> starttemp;
cout << "eindtemperatuur in celius: ";
cin >> endtemp;
int nlength = (endtemp - starttemp) / step;
int *arrayc[nlength];
creatingarray(starttemp, arrayc, nlength, step);
/*
for (int accumulater = 0; accumulater < endtemp)
cout << startemp;
temperature S("test");*/
cout << nlength;
cout << "CELSIUS" << endl;
for (int i=0;i<nlength;i++)
cout<< *arrayc[i] << " ";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效创建阵列(int STARTETEMP2、int*arrayc2[],int nlength2、int step2){
int*newtemp=newint;
*newtemp=starttemp2;
//cout这应该能让你走了
这可能不完全是你打算做什么,但你打算做什么还不清楚
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
int step;
int starttemp;
int endtemp;
cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step";
cout << "step: ";
cin >> step ;
cout << "begintemperatuur in celsius: ";
cin >> starttemp;
cout << "eindtemperatuur in celius: ";
cin >> endtemp;
int const nlength = (step + endtemp - starttemp) / step;
vector<int> arrayc( nlength ); // Important: don't use curly braces here! See comment by M.M.
for( int i = 0; i < nlength; ++i )
{
arrayc[i] = starttemp + i*step;
}
cout << nlength << " temperatures in CELSIUS:\n";
cout << "\n";
for( int i = 0; i < nlength; ++i )
{
cout << arrayc[i] << "\n";
}
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数步;
int starttemp;
内部温度;
步履蹒跚;
cout>starttemp;
cout>endtemp;
int const nlength=(步骤+结束温度-开始温度)/步骤;
vector arrayc(nlength);//重要提示:此处不要使用大括号!请参阅M.M.的评论。
对于(int i=0;i cout由于您处理的是可变长度数组,因此需要使用new[]
对其进行分配,然后才能将数据读入其中。例如:
#include <iostream>
#include <limits>
using namespace std;
void creatingarray(int starttemp, int *arrayc, int nlength, int step)
{
int newtemp = starttemp;
for (int i = 0; i < nlength; ++i)
{
arrayc[i] = newtemp;
newtemp += step;
}
}
int main()
{
int step;
int starttemp;
int endtemp;
cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step." << endl;
cout << "step: ";
cin >> step;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "begintemperatuur in celsius: ";
cin >> starttemp;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "eindtemperatuur in celius: ";
cin >> endtemp;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int nlength = (endtemp - starttemp) / step;
int *arrayc = new int[nlength];
creatingarray(starttemp, arrayc, nlength, step);
cout << "Length " << nlength << endl;
cout << "CELSIUS" << endl;
for (int i = 0; i < nlength; ++i)
cout << arrayc[i] << " ";
delete[] arrayc;
return 0;
}
您最好使用std::vector
。首先,您的代码不会编译,因为int*arrayc[nlength];
,您不能用非常量值指定数组大小。@Zhou-有些编译器允许您使用C中的VLA作为扩展。假设开始温度为5,结束温度为7,步长为1。然后值的数量为3。但是(7-5)/1=2。正如@BoPersson所说,只要使用std::vector
,除非你被禁止使用它。是吗?让我们说我对vector还没有任何线索!所以我认为这是更好地理解此引用工作原理的一个好方法。(代码编译。数组的长度在初始化时已知)。不正确的步骤不是问题,只要它低于最终温度。代码的这一部分没有问题。只是如何获得内存位置以正确写入数组作为引用?我希望有人能给出答案(是的,因此我将研究向量)谢谢,正如我所看到的,这将完全满足我的需要,但以另一种方式:)我想在c++中通过引用构建数组是不可能的,这是可能的,只是不是你现在所做的方式。vector arrayc{nlength};
生成一个带有1个值元素的向量nlength
(首选初始值设定项列表构造函数)@M.M:谢谢,修好了!修好了。:)
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
void creatingarray(int starttemp, std::vector<int> &arrayc, int nlength, int step)
{
int newtemp = starttemp;
for (int i = 0; i < nlength; ++i)
{
arrayc.push_back(newtemp);
newtemp += step;
}
}
int main()
{
int step;
int starttemp;
int endtemp;
cout << "Geef begin en eind temperatuur in om om te zetten met een bepaalde step." << endl;
cout << "step: ";
cin >> step;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "begintemperatuur in celsius: ";
cin >> starttemp;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "eindtemperatuur in celius: ";
cin >> endtemp;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int nlength = (endtemp - starttemp) / step;
std::vector<int> arrayc;
arrayc.reserve(nlength);
creatingarray(starttemp, arrayc, nlength, step);
cout << "Length " << nlength << endl;
cout << "CELSIUS" << endl;
for (int i = 0; i < nlength; ++i)
cout << arrayc[i] << " ";
return 0;
}