简体   繁体   English

指针未传递其价值

[英]Pointers not passing their value

I'm trying to solve a mystery that occured while doing the assignement. 我正在尝试解决分配过程中发生的一个谜。 The main and entry functions works just fine, but the average one doesnt. 主函数和入口函数工作正常,但平均水平却没有。 While checking if variables constant and arraysize, it seems that they do not to pass it's values like it should while I was dereferencing them. 在检查变量是否为常数和arraysize时,似乎它们没有像我解引用它们时那样传递其值。

And yes, i'm new to pointers, so any suggestions would be great. 是的,我是新手,所以任何建议都会很棒。

Here's the code: 这是代码:

#include <iostream>
using namespace std;
void entry(int &size, int *arraysize, int &c, int *constant, int *firstArray, int *secondArray);
void average(int &size, int &c, int *arraysize, int *constant, int *firstArray, int *secondArray);
void newarray();
void output();
int main() {
    int size;
    int *arraysize;
    int c;
    int *constant;
    int *firstArray;
    int *secondArray;
    entry(size, arraysize, c, constant, firstArray, secondArray);
    average(size, c, arraysize, constant, firstArray, secondArray);
}

void entry(int &size, int *arraysize, int &c, int *constant, int *firstArray, int *secondArray) {
    cout<<"Enter the size of the array: ";
    cin>>size;
    arraysize = &size;
    cout<<"array size: "<<*arraysize<<endl;
    cout<<"Enter the constant c: ";
    cin>>c;
    constant = &c;
    cout<<"constant c size: "<<*constant<<endl;
    firstArray = new int[*arraysize];
    secondArray = new int [*arraysize];
    for (int i=0; i<*arraysize; i++) {
        cout<<"Enter the "<<i+1<<" element of the first row: ";
        cin>>firstArray[i];
    }
    for (int i=0; i<*arraysize; i++) {
        cout<<"Enter the "<<i+1<<" element of the second row: ";
        cin>>secondArray[i];
    }
}

void average(int &size, int &c, int *arraysize, int *constant, int *firstArray, int *secondArray) {
    cout<<"Array size: "<<*arraysize<<endl;
    cout<<"Constant: "<<*constant<<endl;
}

It show's me this kind of error, when program hits the average function http://i.imgur.com/esF9c04.png 当程序命中平均功能时,它会向我显示这种错误http://i.imgur.com/esF9c04.png

Modifying arguments in callee won't affect caller's local variables unless the arguments modified are reference. 除非被修改的参数是引用的,否则在被调用方中修改参数不会影响调用方的局部变量。

Use reference as you did in int arguments. 像在int参数中一样使用引用。

Modify both prototype declaration and function definition like this: 修改原型声明和函数定义,如下所示:

void entry(int &size, int *&arraysize, int &c, int *&constant, int *&firstArray, int *&secondArray)

There is some errors in your code. 您的代码中有一些错误。

When you want to send values to a function/procedure and save their changes: 当您要将值发送到函数/过程并保存其更改时:

In C++ only: 仅在C ++中:

  • For variables: When receiving int &variable and when sending send(variable) 对于变量:接收int&variable时以及发送send(variable)时
  • For arrays: When receiving int *array and when sending send(array) 对于数组:接收int * array和发送send(array)时

In C: 在C中:

  • For variables: When receiving int *variable and when sending send(&variable) 对于变量:接收int * variable和发送send(&variable)时
  • For arrays: When receiving int *array and when sending send(array) 对于数组:接收int * array和发送send(array)时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM