为什么输出不正确
我有这个代码,但我不知道out
的结果是什么不正确的?
int *getarray(int *out)
{
int data[20];
for(int i=0;i<20;i++)
{
data[i]=i;
}
for(int i=0;i<20;i++)
{
out+=data[i];
}
Serial.printf("out inside getarray = %d",out);
return data;
}
void resultdata(){
int *n;
int out;
n=getarray(&out);
Serial.printf("out = %d",out);
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
resultdata();
}
I have this code but I don't know what happen why the result of out
is incorrect any advice please?
int *getarray(int *out)
{
int data[20];
for(int i=0;i<20;i++)
{
data[i]=i;
}
for(int i=0;i<20;i++)
{
out+=data[i];
}
Serial.printf("out inside getarray = %d",out);
return data;
}
void resultdata(){
int *n;
int out;
n=getarray(&out);
Serial.printf("out = %d",out);
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
resultdata();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个问题:
数据
getArray中的数组
一旦getArray
函数完成执行,就会停止存在。out
才能在resultdata
中更改OUT
。再次阅读有关学习材料中指针的章节。您可能想要这样的东西:
Two problems:
data
array ingetarray
will cease to exist once thegetarray
function has finished execution.out
in order to change theout
inresultdata
. Read again the chapter dealing with pointers in your learning material.You probably want something like this: