为什么输出不正确

发布于 2025-02-11 03:29:13 字数 519 浏览 1 评论 0原文

我有这个代码,但我不知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-02-18 03:29:13

两个问题:

  • 数据 getArray中的数组一旦getArray函数完成执行,就会停止存在。
  • 您需要取消out才能在resultdata中更改OUT。再次阅读有关学习材料中指针的章节。

您可能想要这样的东西:

int *getarray(int *out, int *data)  
{  
    // remove this   int data[20];  
    *out = 0;            // you need to dereference the out pointer
                         // and initialize it to 0

    for (int i = 0; i < 20; i++)  
    {  
       data[i] = i;      // you need to dereference the out pointer
       *out += data[i]; 
    } 

    // I'm not sure why you have this second loop here,
    // it looks pretty pointless
    for (int i = 0; i < 20; i++)  
    {  
       *out += data[i];  // you need to dereference the out pointer
    }

    Serial.printf("*out inside getarray = %d", *out);

    return data;  
}

void resultdata(){
  int *n;  
  int out;
  int data[20];              // declare the data array here
  n = getarray(&out, data);  // pass address of the data array
                             // so that getarray can fill the data array
  Serial.printf("out = %d", out);   
} 

Two problems:

  • The data array in getarray will cease to exist once the getarray function has finished execution.
  • You need to dereference out in order to change the out in resultdata. Read again the chapter dealing with pointers in your learning material.

You probably want something like this:

int *getarray(int *out, int *data)  
{  
    // remove this   int data[20];  
    *out = 0;            // you need to dereference the out pointer
                         // and initialize it to 0

    for (int i = 0; i < 20; i++)  
    {  
       data[i] = i;      // you need to dereference the out pointer
       *out += data[i]; 
    } 

    // I'm not sure why you have this second loop here,
    // it looks pretty pointless
    for (int i = 0; i < 20; i++)  
    {  
       *out += data[i];  // you need to dereference the out pointer
    }

    Serial.printf("*out inside getarray = %d", *out);

    return data;  
}

void resultdata(){
  int *n;  
  int out;
  int data[20];              // declare the data array here
  n = getarray(&out, data);  // pass address of the data array
                             // so that getarray can fill the data array
  Serial.printf("out = %d", out);   
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文