Problem using pointer...

N

nabeel.girgis

I am passing a vector by reference into a function and I am trying to
use a pointer in that function.

I get an error saying : '=' : cannot convert from 'std::vector<_Ty> *'
to 'int *'

when I try to initialize the pointer to point to the vector. This is
my first time using pass by reference into a function while trying to
declare a pointer in the same function. My code is given below.


void v_abs(vector<int>&x)
{
int *y;

**** y = &x;

while (*y)
{
if (*y < 0)
*y *= -1;

y++;
}
}

The *** represent where the cursor flashes when I double click on the
error message. I am a student and I know that the standard way of
declaring and initializing a pointer is:
int *ptr;

ptr = &element;

But I am having trouble initializing a vector passed by reference.
Thank you.
 
A

Andre Kostur

I am passing a vector by reference into a function and I am trying to use
a pointer in that function.

I get an error saying : '=' : cannot convert from 'std::vector<_Ty> *' to
'int *'

when I try to initialize the pointer to point to the vector. This is my
first time using pass by reference into a function while trying to declare
a pointer in the same function. My code is given below.


void v_abs(vector<int>&x)
{
int *y;

**** y = &x;

&x is a pointer to a vector of int, not a pointer to an int. A vector is
not simply an array. Now what may work (and IIRC, as of TR1 is Standard):

y = &x[0];
while (*y)

Umm... while the value which y points as is non-zero.... wouldn't that:
a) prematurely end your loop if any of your data points are zero?
b) invoke Undefined Behaviour if none of your data points are zero (as
your loop would run off the end of the array)?
{
if (*y < 0)
*y *= -1;

y++;
}
}

The *** represent where the cursor flashes when I double click on the
error message. I am a student and I know that the standard way of
declaring and initializing a pointer is:
int *ptr;

ptr = &element;

But I am having trouble initializing a vector passed by reference. Thank
you.



Why aren't you using the iterator version of this loop? :

void v_abs(vector<int> & v)
{
for (vector<int>::iterator where = v.begin(); where != v.end(); ++where)
{
if (*where < 0)
{
*where *= -1;
}
}
}


Or better yet, use algorithms (such as std::transform). But I leave this
implementation as an exercise for the reader.
 
I

Ian Collins

I am passing a vector by reference into a function and I am trying to
use a pointer in that function.

I get an error saying : '=' : cannot convert from 'std::vector<_Ty> *'
to 'int *'

when I try to initialize the pointer to point to the vector. This is
my first time using pass by reference into a function while trying to
declare a pointer in the same function. My code is given below.


void v_abs(vector<int>&x)
{
int *y;

**** y = &x;
The address of x (&x) is a pointer to vector, not a pointer to int.

Use an iterator instead:

for( std::vector<int>::iterator y = x.begin(); y != x.end(); ++y )
{
if (*y < 0)
{
*y *= -1;
}
}
 
R

red floyd

Ian said:
The address of x (&x) is a pointer to vector, not a pointer to int.

Use an iterator instead:

for( std::vector<int>::iterator y = x.begin(); y != x.end(); ++y )
{
if (*y < 0)
{
*y *= -1;
}
}

I believe the canonical method for turning a vector into a pointer
(usually for passing to a legacy API) is

std::vector<T> v;
// fill v

T* pT = &v[0];

I believe that as of TC1 (C++03), the vector's storage is guaranteed to
be contiguous.
 
H

Howard Gardner

/*
v_abs is your function fixed. I left your
original code for reference.

v_abs_nml is another way to write the same
function. Probably the most common form.

v_abs_std is another way to write the same
function, using standard library components.
It does exactly what your function does.
I'd almost bet that writing that version
of the function is your next assignment.

i_print prints a single integer on cout.
The calls to for_each in main use it
to print out the entire vector.
*/

#include <fstream> // cout, endl
#include <vector> // vector
#include <algorithm> // for_each, transform
#include <cstdlib> // abs
using namespace std;

void v_abs(vector<int>&x)
{
// int *y;
// **** y = &x;
vector< int >::iterator y = x.begin();

// while (y )
while( y != x.end() )
{
if (*y < 0)
*y *= -1;

y++;
}
}

void v_abs_nml(vector<int>&x)
{
for( vector< int >::iterator i = x.begin(); i != x.end(); ++i )
{
*i = abs( *i );
}
}

void v_abs_std( vector< int > & x )
{
transform( x.begin(), x.end(), x.begin(), abs );
}

void i_print( int x )
{
cout << x << endl;
}

int main()
{
vector< int > a;
a.push_back( -1 );
a.push_back( -2 );
a.push_back( -3 );

cout << "before" << endl;
for_each( a.begin(), a.end(), i_print );

v_abs( a );
// v_abs_nml( a );
// v_abs_std( a );

cout << "after" << endl;
for_each( a.begin(), a.end(), i_print );
}
 
I

Ian Collins

red said:
Ian said:
The address of x (&x) is a pointer to vector, not a pointer to int.

Use an iterator instead:

for( std::vector<int>::iterator y = x.begin(); y != x.end(); ++y )
{
if (*y < 0)
{
*y *= -1;
}
}

I believe the canonical method for turning a vector into a pointer
(usually for passing to a legacy API) is

std::vector<T> v;
// fill v

T* pT = &v[0];

I believe that as of TC1 (C++03), the vector's storage is guaranteed to
be contiguous.

True, but if you are going to iterate over the vector, iterators are the
way to go.
 
O

Old Wolf

red said:
I believe the canonical method for turning a vector into a pointer
(usually for passing to a legacy API) is

std::vector<T> v;
// fill v

T* pT = &v[0];

I believe that as of TC1 (C++03), the vector's storage is guaranteed to
be contiguous.

You have to be careful with this method, because if v is empty
then &v[0] causes undefined behaviour.
 
R

red floyd

Old said:
red said:
I believe the canonical method for turning a vector into a pointer
(usually for passing to a legacy API) is

std::vector<T> v;
// fill v

T* pT = &v[0];

I believe that as of TC1 (C++03), the vector's storage is guaranteed to
be contiguous.

You have to be careful with this method, because if v is empty
then &v[0] causes undefined behaviour.

Hence the "fill v" comment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top