1

The problem is passing lists/vectors by reference

   int main(){
        list<int> arr;
        //Adding few ints here to arr
        func1(&arr);   
        return 0; 
    }

    void func1(list<int> * arr){
     // How Can I print the values here ?

     //I tried all the below , but it is erroring out.
     cout<<arr[0]; // error
     cout<<*arr[0];// error
     cout<<(*arr)[0];//error

     //How do I modify the value at the index 0 ?

     func2(arr);// Since it is already a pointer, I am passing just the address
     }

     void func2(list<int> *arr){

     //How do I print and modify the values here ? I believe it should be the same as above but 
     // just in case.
     }

Is the vectors any different from the lists ? Thanks in advance.

Any links where these things are explained elaborately will be of great help. Thanks again.

Ray Hidayat
  • 16,055
  • 4
  • 37
  • 43
bsoundra
  • 930
  • 2
  • 13
  • 27
  • This won't even compile. – Prasoon Saurav Jan 08 '11 at 07:21
  • @prasoon : By errors in the comments, it meant the compile-time errors. Any solution on how to approach it in the right/ proper way ? – bsoundra Jan 08 '11 at 07:24
  • I meant apart from the errors mentioned the code has few more errors. Return types of `main`, `func1` and `func2` are missing. `main` should return an `int`. – Prasoon Saurav Jan 08 '11 at 07:28
  • I was actually at using the []operator mainly. But I just now found that it is available only with vector but not the lists. Thanks all. – bsoundra Jan 08 '11 at 07:34
  • Incidentally, I should point out that you're not _really_ passing it by reference. If you're using C++, you should consider using an actual reference, with a function of the form `void func1(list & arr)`, etc. – Mike Caron Jan 08 '11 at 07:36
  • @mike : Sorry, you are saying that the above syntax for the function call and the definition are wrong and it does not pass-by-reference ? I just now tried a piece of code with the same syntax following the same function call and definition. I worked perfectly as pass-by-reference P.S. How to add code in the comments ?Is it not possible . I tried to give the above exmaple. But It is not in the code format – bsoundra Jan 08 '11 at 07:40
  • 1
    @bsoundra: Well, it is passing by pointer, which _is_ by reference, but what happens if you pass NULL, etc. If you pass a real reference, then you don't have to worry about NULL or anything like that. See this for more details: http://stackoverflow.com/questions/3224155/c-difference-between-reference-objects-and-pointers – Mike Caron Jan 08 '11 at 07:43

2 Answers2

4

You aren't passing the list by reference, but by a pointer. In "C talk" the two are equal, but since there is a reference type in C++, the distinction is clear.

To pass by reference, use & instead of * - and access "normally", i.e.

void func(list<int>& a) {
    std::cout << a.size() << "\n";
}

To pass by pointer, you need to derefer the pointer with an asterisk (and do take note of operator presedence), i.e.

void func(list<int>* arr) {
    std::cout << (*a).size() << "\n"; // preferably a->size();
}

There is no operator[] in std::list.

eq-
  • 9,986
  • 36
  • 38
2
   //note the return type also!
   void func1(list<int> * arr)
   {
    for (list<int>::iterator i= arr->begin() ; i!= arr->end(); i++ )
    {
          //treat 'i' as if it's pointer to int - the type of elements of the list!
          cout<< *i << endl;
    }
   }

In your example, return type of func1() is not specified. So I specified it. You may change from void to some other type. Also don't forget to specify return type for func2() and main() too.


If you want to use subscript operator [], then you've to use std::vector<int>, as list<> doesn't overload operator[]. In that case, you can write :

for(std::vector<int>::size_type i = 0 ; i < arr->size() ; i++ )
{
    cout << (*arr)[i] << endl;
}

I'm still assuming arr is pointer to vector<int>.

Maybe, you would like to modify your code a little bit, like this:

   void func1(vector<int> & arr) // <-- note this change!
   {
         for(std::vector<int>::size_type i = 0 ; i < arr.size() ; i++ )
         {
                cout << arr[i] << endl;
         }
   }
Nawaz
  • 353,942
  • 115
  • 666
  • 851