Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing reference to pointer

Reply
Thread Tools

passing reference to pointer

 
 
softdeveloper007
Guest
Posts: n/a
 
      02-15-2009
Hi,

I am stuck here and need some inputs. To cut it short and simple:

void func1( )
{
int* p1, p2;
....

func2 (p1, p2);
...

}

void func2 (int*& pt1, int*& pt2)
{

}

I got: "error LNK2001: unresolved external symbol...fatal error
LNK1120: 1 unresolved externals"

but problem is gone by simply changing it to ptr to ptr:

void func1( )
{
int* p1, p2;
....

func2 (&p1, &p2);
...

}

void func2 (int** pt1, int** pt2)
{

}

compiler: visual studio6.0.

Any pointer, please? thanks!
 
Reply With Quote
 
 
 
 
Gareth Owen
Guest
Posts: n/a
 
      02-15-2009
softdeveloper007 <(E-Mail Removed)> writes:

> void func1( )
> {
> int* p1, p2;


This doesn't declare two pointers, it declares a p1 as a
pointer-to-int and p2 as an int.

int *p1, *p2;

does what you want. It's good practice to group the * with the
variable name for precisely that reason.
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      02-15-2009
Gareth Owen wrote:
> softdeveloper007 <(E-Mail Removed)> writes:
>
>> void func1( )
>> {
>> int* p1, p2;

>
> This doesn't declare two pointers, it declares a p1 as a
> pointer-to-int and p2 as an int.
>
> int *p1, *p2;
>
> does what you want. It's good practice to group the * with the
> variable name for precisely that reason.


And an even better practice is to only define one variable at a time.

int* p1 = 0;
int* p2 = 0;


Bo Persson


 
Reply With Quote
 
softdeveloper007
Guest
Posts: n/a
 
      02-16-2009
guys, thanks for all your inputs.

first of all, i'd like to apologize for the typo of missing * ahead of
p2 declaration that causes confusion.

i tried to make it as simple as possible. the original problem was
bubble-sorting the linked list by passing two pointers to nodes to a
swapping function.

it seems to me that, in order to properly step in the original
pointers thru the list, the two pointers should be passed by value
instead of reference to pointers,in other words, the pointers in the
caller would remain pointing to the same memory location, and not be
affected by swapping function, sounds right, right?

but it seemed, the pace that the pointer walks in the caller is
affected by the swapping function and thus steps in 2 steps (instead
of 1) each time due to some reason.

i knew simply swapping the value instead of re-arranging the nodes
would be a much simpler way but that's not my choice here due to other
restrictions.

thanks.

On Feb 15, 4:11 am, "Bo Persson" <(E-Mail Removed)> wrote:
> Gareth Owen wrote:
> > softdeveloper007 <(E-Mail Removed)> writes:

>
> >> void func1( )
> >> {
> >> int* p1, p2;

>
> > This doesn't declare two pointers, it declares a p1 as a
> > pointer-to-int and p2 as an int.

>
> > int *p1, *p2;

>
> > does what you want. It's good practice to group the * with the
> > variable name for precisely that reason.

>
> And an even better practice is to only define one variable at a time.
>
> int* p1 = 0;
> int* p2 = 0;
>
> Bo Persson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments