473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem using pointer...

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.

Apr 30 '06 #1
7 1945
On Sat, 29 Apr 2006 20:58:38 -0700, nabeel.girgis wrote:
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.
Apr 30 '06 #2
na***********@gmail.com wrote:
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;
}
}

--
Ian Collins.
Apr 30 '06 #3
Ian Collins wrote:
na***********@gmail.com wrote:
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;
}
}


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.
May 1 '06 #4
/*
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 );
}
May 1 '06 #5
red floyd wrote:
Ian Collins wrote:
na***********@gmail.com wrote:
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;
}
}


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.

--
Ian Collins.
May 1 '06 #6
red floyd wrote:

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.

May 1 '06 #7
Old Wolf wrote:
red floyd wrote:
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.
May 1 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
37
by: Patrik Huber | last post by:
Hello! I got the following Code in Assembler (NASM), which prints out "5" in realmode: mov ax, 0xB800 mov es, ax mov byte , '5' I want to do the same in gcc now, but I'm stuck. GCC...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
14
by: mauri1106 | last post by:
Hi, I have a little problem with a pointer. In my project is included an ".h" file with this declaration: "#define pMDMA_D0_START_ADDR ((void * volatile *)MDMA_D0_START_ADDR)" If I assign a...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
1
by: ilieyah | last post by:
Hi! Sorry for the silly title, but I just couldn't come up with anything sensible. I have some test code here to demonstrate an issue I'm having. What I'm trying to do is implement a reference...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.