将指针数组地址传递给函数并更新地址中的数据

Pass pointer array address to function and update data in the address

本文关键字:地址 更新 函数 数据 指针 数组 址传      更新时间:2023-10-16

我在理解和使用指针方面相当薄弱。所以,请帮帮我。

我的目标是将数组指针的地址传递给函数,(即)指针指向的地址,并在函数中使用"*"操作符直接更新地址中的值,以避免任何返回值。此外,该数组的长度必须在传递给它的函数中动态更改。这是我的尝试。如果有更好的方法来更新变量的值,而不需要从函数返回,请务必提到这一点来帮助我。

但是我得到错误,因为我知道我做错了,但仍然想尝试我所知道的,因为我认为最好的学习方式是做和犯尽可能多的错误。请帮我一下。

这是main函数

     int main()
     {
        double *trans;
        int *rots;
        readctrls(rots,trans);
        for(int i=0;i<trans.size();i++)
        {
                cout<<trans[i]<<endl<<rots[i];
        }
     }

在这里,我试图将指针数组的地址传递给函数readctrl。然后,打印它的值。我没有提到大小,因为它将在稍后的函数中确定。

函数只是从文本文件中逐行读取数字,并将这些数字存储在这两个数组中。readctrl函数如下:

    void readctrls(int*& rots,double*& trans)
    {
        fstream inputs;
        inputs.open("input_coods.txt");
        int nol = 0,i = 0;
        string line,temp,subtemptrans,subtemprots;
        while(getline(inputs,line))
        {
                ++nol;
        }
        cout<<nol<<endl;
        inputs.close();
        inputs.open("input_coods.txt");
        string *lines = new (nothrow) string[nol];
        trans = new double[nol];
        rots = new int[nol];
        for(int i = 0; i<nol ; i++)
        {
                getline(inputs,lines[i]);
                temp = lines[i];
                for(int j = 0; j<temp.length() ; j++)
                {
                        if(temp.at(j) == ' ')
                        {
                                subtemptrans = temp.substr(0,j);
                                subtemprots = temp.substr(j+1,temp.length()-j);
                                trans[j] = ::atof(subtemptrans.c_str());
                                rots[j] = atoi(subtemprots.c_str());
                        }
                }
        }
        inputs.close();
}

非常感谢你们的帮助。我能够理解一点,改变代码,现在能够编译没有错误。然而,我从文件中读取并加载到数组中的值似乎并没有在main中得到反映。当我在函数中打印数组时,我从文件中获得正确的值,但是当我在main()中打印时,我得到零。请帮我一下。

这些是文件

的内容
     0.2 0 
     0.2 0 
     0.2 0
     0.2 0
     0.2 0

而打印'trans',它每行取第一个数字,在函数中,我得到正确的值。但是在main函数

中打印时
    0
    0
    0
    0.2.

在传递给函数时将指针更改为指针引用。请在功能代码中勾选编辑。

声明

void readctrls(int &rots,double &trans)

告诉编译器rotstrans单个值的引用。它们是而不是指针。

更糟糕的是,在调用这个函数时,实际上是在试图传递一个指针对指针的参数。

你应该修改声明,让它实际接受指针:

void readctrls(int* rots, double* trans)

则更改调用以不使用地址操作符(因为它们已经是指针):

readctrls(rots, trans);

您的代码有几个错误。以下是其中一些:

 double *trans = new double[];
 int *rots = new int[]; //^^You need to give the size 
 for(int i=0;i<trans.size();i++)
 {
     cout<<*trans[i]<<endl<<*rots[i];
 }

transrots是简单的双精度和整数数组,您只需使用trans[i]来打印第i个元素。动态数组的使用应该类似于静态数组。看一下这个指针和数组来获得一些基本的理解。同时,看看c++中的动态内存,对这一点有一些理解。

void readctrls(int &rots,double &trans);
//^^expects reference to int and double while you are not passing int and double from main

数组和指针可以被看作是引用内存中的范围的类似方式。如果你想通过指针来引用一个内存范围,那么只需将指针传递给函数,即

double pd* = new double[10];
fun(pd);

 void fun(double* pd, int numDoubles)
    {
       do {
       double d = magicDoubleGenerator();
       *pd = d; // read as "the value that pd points to" or "contents of pd"
       } while (++pd < pd + numDoubles);
    }

指针很难,直到有一天你意识到"啊!他们只是用指向事物!"

有很多错误…

inputs.open("input_coods.txt"); // second argument is missing

check this fstream open

void readctrls(int &rots,double &trans)

改为

void readctrls(int* rots, double* trans) // this creates pointer rots trans
*trans = new double[nol]; // remove *
*rots = new int[nol]; // remove *
double *trans = new double[]; // not specified the size
int *rots = new int[]; // not specified the size
readctrls(&rots,&trans); // this means you passing address of pointer  
trans.size() ; // this is c++  double is not a class

我推荐你从这个网站学习c++教程