views:

144

answers:

5

I have this code, but it won't compile and i can't understand what is wrong - i guess the pointering of the vector is not correct. My idea was to collect some numbers in main() and store them in a vector and array, and then pass the memory address of them to a function, and using a pointers to print the data stored.

I came up with this when i read something about pointers which said that i should use them in order to save memory, so IMO the code below will not copy the contents of the vector and the array but use a pointer to access their location in memory - that's what i want to do.

#include <iostream>
#include <vector>
using namespace std;

void function(vector<int>* a, int *s)
{
    cout << "function starts.." << endl;
    for(int i=0;i<a->size();i++)
    {
        cout << a[i] << endl;
        cout << s[a[i]] << endl;
    }
    cout << "function ends..." << endl;
}


int main(void)
{
    vector<int> m;
    int s[102];
    for(int i=0;i<10;i++)
    {
        m.push_back(i*i);
        s[i*i] = i-2;
    }
    function(&m, &s);
    return 0;
}

I receive several errors on compiling, something is wrong.

Please tell me what's wrong with my code and how to fix it. thank you...

+5  A: 

You should pass the vector by reference, not by pointer:

void function(vector<int>& a, int *s)

And then

function(m, ...);

Using [] on a pointer to a vector would certainly cause strange problems because it behaves as if a pointed to an array of std::vectors (while it actually only points to one). The vectors itself are never indexed by that. You could also use (*a)[...] to index the vector by the pointer.

Tronic
make sure when you make the change above to make the call like this: `function(m, s);`
Charles Beattie
+2  A: 

if you insist in parsing by pointer then the correct syntax shoulld be:

void function(vector<int>* a, int *s[]) 
{ 
    cout << "function starts.." << endl; 
    for(int i=0;i<a->size();i++) 
    { 
        cout << (*a)[i] << endl; 
        cout << (*s)[(*a)[i]] << endl; 
    } 
    cout << "function ends..." << endl; 
} 
Charles Beattie
error: cannot convert 'int (*)[102]' to 'int**'It's not working.
VaioIsBorn
Sorry read AndreyT comment below. But please call funtion(m,s) and parse by reference instead.
Charles Beattie
+1  A: 

(corrected)

&s is in fact int(*)[102]: pointer to a pointer to an array of 102 items.

You should just say

function(&m, s);

This is because by old C legacy rule, an array is essentially a const pointer to its item with index 0. So s is already int*

Vlad
AndreyT
Vlad
+1  A: 

This version works:

#include <iostream>
#include <vector>
using namespace std;

void function(const vector<int>& a, int s [102])
{
    cout << "function starts.." << endl;
    for(int i=0;i<(int)a.size();i++)
    {
        cout << a [i] << endl;
        cout << s[a [i]] << endl;
    }
    cout << "function ends..." << endl;
}


int main(void)
{
    vector<int> m;
    int s[102];
    for(int i=0;i<10;i++)
    {
        m.push_back(i*i);
        s[i*i] = i-2;
    }
    function(m, s);
    return 0;
}
ileon
Nice, it works. But as i can see s is not passed as pointer - why ? I would also like to pass a pointer to s in the function so i don't pass the whole array to the function - is there any way to do that ?
VaioIsBorn
Passing the whole array by value is not good practice. I'd use"void function(const vector<int>"
codebolt
@VaiolsBorn: It is passed as a pointer - you can't pass an array by value in C++. `int s[102]` in the parameters is completely equivalent to `int s[]` (the size is ignored anyway) and `int* s` (array decays to pointer anyway).
visitor
Here it is:void function(const vector<int> for(int i=0;i<(int)a.size();i++) { cout << a [i] << endl; cout << s[a [i]] << endl; } cout << "function ends..." << endl;}
ileon
A: 

First of all in the main program s is a pointer to an int, while m is a vector. Thus the function call should be as follows:

function(&m, s);

Secondly in the function a is a pointer to a vector, so should be indexed as follows: (*a)[i].

However you should really be using const references to pass your vector around:

void function(const vector& a, int *s) { .. cout << a[i] << endl; .. }

And call it like:

function(m, s);

Conor