0

I am pretty weak in understanding and working with pointers. So, Please help me here.

My objective is to pass an array pointer's address to a function ,(i.e.) the address the pointer is pointing to, and update the values directly in the address using the '*' operator, in the function, to avoid any return values. Moreover, the length of this array has to be changed dynamically in the function to which it is passed. This is my attempt. If there's a better method to update the value of an variable, without having it returned from a function, please do mention that to help me.

But am getting errors, as I know I am doing it wrong, but still wanted to try with what I know, since I thought the best way to learn is to do and make as many mistakes as possible. Please help me here

This is the main function

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

Here, am trying to pass the address of the pointer arrays to the function readctrls. then later, print its values. I haven't mentioned a size, cuz it will be determined later in the function.

The function is just to read numbers from a text file, line by line and store these numbers in these 2 arrays. The readctrls function is as follows.

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

Thanks a lot for your help guys. I was able to understand a bit and changed the code and was able to compile now without errors. however, the value I read from file and load into the array, doesn't seem to get reflected back in the main. Am getting the correct values from the file when I print the array in the function, but am getting zeros, when I print in the main(). Please help me here.

These are the contents of the file

     0.2 0 
     0.2 0 
     0.2 0
     0.2 0
     0.2 0

while print 'trans', which takes the first number every line, in the function, am getting the correct values. But while printing in the main function

    0
    0
    0
    0.2.

I changed the pointer to pointer reference while passing to function. Please check edit in the function code. Thanks in advance.

3
  • 4
    If you're "pretty weak with pointers" (and in general really), prefer standard library containers over them. Commented May 9, 2013 at 18:19
  • 1
    I actually wanted to do this to get myself to understand pointers. That's why. Am sorry if this question tested your patience. Apologies. Commented May 9, 2013 at 18:24
  • 1
    pointer codes are way more annoying to read then write :) Commented May 9, 2013 at 18:26

4 Answers 4

1

The declaration

void readctrls(int &rots,double &trans)

tells the compiler that rots and trans are references to a single value each. They are not pointers.

To make matters worse, you are actually trying to pass a pointer-to-pointer as arguments when calling this function.

You should change the declaration to actually take pointers:

void readctrls(int* rots, double* trans)

then change your call to not use the address-of operator (as those are already pointers):

readctrls(rots, trans);
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your reply. Could you please elaborate the points? I declared 2 pointer arrays and then passed the address to the function and tried to receive the address. Could you please temme the right way to do it, if this is rong? I know its rong.
@LakshmiNarayanan In your call, by using the address-of operator & you pass the address of the pointer when you should just pass the pointer.
I get it. thanks. :). am implementing the changes and seeing wat I can do here. :). I get a bit of understanding now.
@LakshmiNarayanan Okay, you allocate both before calling the function and in the function. You should do it only once. If you do it before calling the function, then that should be enough. But if you do it in the function, then you need to pass the pointers as references: void readctrls(int*& rots, double*& trans) { ... }
@LakshmiNarayanan In the code in your question you do int* rots = new int[10];. Then inside the function you allocate again: rots = new int[nol];. You shouldn't do both, you have to use only one.
|
1

Your code has several errors. Here are some of them:

 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 and rots are simply array of double and integers, you simply use trans[i] to print the i-th element. Dynamic arrays should be used similarly to static arrays. Take a look at this pointer and arrays for some basic understanding. Meanwhile, look at dynamic memory in C++ for some understanding on this point.

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

2 Comments

thanks for those links and explanation. was able to make few changes and get it compiled. still stuck with a small issue. please help me there. thanks again.
@LakshmiNarayanan glad that it helped a bit.
1

An array and a pointer can be thought about similarly as a way of referring to a range in memory. If you want to refer to a range of memory via pointers, then just pass the pointer to the function, ie

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

    }

Pointers are hard until one day you realize "Ahh! they just point at things!"

Comments

1

There are many errors ...

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

check this fstream open

void readctrls(int &rots,double &trans)

change to

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

I am recommending you to study c++ from this site C++ Tutorial

1 Comment

Perhaps void readctrls(int (*rots)[nol], double (*trans)[nol]), since &rots and &trans are int (*)[nol] and double (*)[nol] respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.