0

我在理解和使用指针方面非常薄弱。所以,请在这里帮助我。

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

但是我遇到了错误,因为我知道我做错了,但仍然想尝试我所知道的,因为我认为最好的学习方法是做尽可能多的错误。请在这里帮助我

这是主要功能

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

在这里,我试图将指针数组的地址传递给函数 readctrls。然后,打印它的值。我没有提到尺寸,因为它将在函数后面确定。

该功能只是从文本文件中逐行读取数字并将这些数字存储在这两个数组中。readctrls 函数如下。

    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() 中打印时,我得到了零。请在这里帮助我。

这些是文件的内容

     0.2 0 
     0.2 0 
     0.2 0
     0.2 0
     0.2 0

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

    0
    0
    0
    0.2.

我在传递给函数时将指针更改为指针引用。请检查功能代码中的编辑。提前致谢。

4

4 回答 4

1

宣言

void readctrls(int &rots,double &trans)

告诉编译器,rots并且每个trans都是对单个值的引用。它们不是指针。

更糟糕的是,您实际上是在调用此函数时尝试将指向指针的指针作为参数传递。

您应该更改声明以实际获取指针:

void readctrls(int* rots, double* trans)

然后将您的调用更改为不使用地址运算符(因为那些已经是指针):

readctrls(rots, trans);
于 2013-05-09T18:20:38.910 回答
1

可以将数组和指针类似地视为引用内存范围的一种方式。如果你想通过指针引用一个内存范围,那么只需将指针传递给函数,即

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);

    }

指针很难,直到有一天你意识到“啊!他们只是指向东西!”

于 2013-05-09T18:32:04.253 回答
1

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

 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];
 }

trans并且rots只是双精度和整数数组,您只需使用它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
于 2013-05-09T18:26:21.113 回答
1

有很多错误...

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

检查这个fstream 打开

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++ Tutorial学习 c++

于 2013-05-09T18:33:20.237 回答