Thread View: att.lang.c++
3 messages
3 total messages
Started by New C Programmer
Sun, 20 Feb 2000 00:00
Losing my pointer
Author: New C Programmer
Date: Sun, 20 Feb 2000 00:00
Date: Sun, 20 Feb 2000 00:00
62 lines
1986 bytes
1986 bytes
I am trying to change the value of a pointer in a function. I successfully
change it in the function but when I get back to the calling function it
returns to the original value.
This is the calling function from llist.cpp (a test harness)
if ( my_llist.i_traverse( from_where, data_ptr ))
{
element = data_ptr->storage;
cout << " Traversed node contained: " << element << ". ";
}else cout << "\nThere was an error, try again.\n";
break;
This is the called function from llist.h.
// *************************************************
// traverse a list, if from_where = 1 then increment data_ptr by one node
else return
// list head in data_ptr.
// *************************************************
BOOLEAN llist::i_traverse ( BOOLEAN from_where, NODE *data_ptr )
{
int pos = 0;
int count;
NODE *top_ptr = LinkedList;
NODE *temp_ptr = LinkedList;
// NODE *temp_ptr2 = *data_ptr;
count = llist::i_size();
if ( count <= 0 )
{
return ( FALSE ); /* position out of range */
}
if (data_ptr == NULL)
{
data_ptr = LinkedList;
return ( TRUE );
}
if ( from_where == 0 )
{
data_ptr = LinkedList;
return ( TRUE );
}
if ( data_ptr->next != NULL )
{
data_ptr = temp_ptr->next;
return ( TRUE );
}
else
{
return ( FALSE );
}
}
Thanks,
Steve
Re: Losing my pointer
Author: Eric
Date: Mon, 21 Feb 2000 00:00
Date: Mon, 21 Feb 2000 00:00
76 lines
2444 bytes
2444 bytes
Hi
While we (someone) work on your problem, have you been to
http://www.inquiry.com/techtips/cpp_pro/
there are lots of problems and answers
From Eric
New C Programmer <s_joanis@siebe-ene.com> wrote in message
news:88q96p$d3f$1@bob.news.rcn.net...
> I am trying to change the value of a pointer in a function. I successfully
> change it in the function but when I get back to the calling function it
> returns to the original value.
>
> This is the calling function from llist.cpp (a test harness)
>
> if ( my_llist.i_traverse( from_where, data_ptr ))
> {
> element = data_ptr->storage;
> cout << " Traversed node contained: " << element << ". ";
> }else cout << "\nThere was an error, try again.\n";
> break;
>
> This is the called function from llist.h.
>
> // *************************************************
> // traverse a list, if from_where = 1 then increment data_ptr by one node
> else return
> // list head in data_ptr.
> // *************************************************
> BOOLEAN llist::i_traverse ( BOOLEAN from_where, NODE *data_ptr )
> {
> int pos = 0;
> int count;
> NODE *top_ptr = LinkedList;
> NODE *temp_ptr = LinkedList;
> // NODE *temp_ptr2 = *data_ptr;
>
>
> count = llist::i_size();
> if ( count <= 0 )
> {
> return ( FALSE ); /* position out of range */
> }
>
> if (data_ptr == NULL)
> {
> data_ptr = LinkedList;
> return ( TRUE );
> }
>
> if ( from_where == 0 )
> {
> data_ptr = LinkedList;
> return ( TRUE );
> }
>
>
> if ( data_ptr->next != NULL )
> {
> data_ptr = temp_ptr->next;
> return ( TRUE );
> }
> else
> {
> return ( FALSE );
> }
> }
>
> Thanks,
>
> Steve
>
>
Re: Losing my pointer
Author: FILEUX Laurent
Date: Thu, 24 Feb 2000 00:00
Date: Thu, 24 Feb 2000 00:00
81 lines
2725 bytes
2725 bytes
Hello,
A pointer is a variable just like the others, exept it is made
to hold an adress to an object, hence if you want to modify it
in a function, you should pass it by reference to allow
modifications.
Simply change your prototype :
BOOLEAN llist::i_traverse ( BOOLEAN from_where, NODE *data_ptr )
to
BOOLEAN llist::i_traverse ( BOOLEAN from_where, NODE * & data_ptr )
// ^^^
// Note the '&'
//
Regards, Laurent.
New C Programmer wrote:
>
> I am trying to change the value of a pointer in a function. I successfully
> change it in the function but when I get back to the calling function it
> returns to the original value.
>
> This is the calling function from llist.cpp (a test harness)
>
> if ( my_llist.i_traverse( from_where, data_ptr ))
> {
> element = data_ptr->storage;
> cout << " Traversed node contained: " << element << ". ";
> }else cout << "\nThere was an error, try again.\n";
> break;
>
> This is the called function from llist.h.
>
> // *************************************************
> // traverse a list, if from_where = 1 then increment data_ptr by one node
> else return
> // list head in data_ptr.
> // *************************************************
> BOOLEAN llist::i_traverse ( BOOLEAN from_where, NODE *data_ptr )
> {
> int pos = 0;
> int count;
> NODE *top_ptr = LinkedList;
> NODE *temp_ptr = LinkedList;
> // NODE *temp_ptr2 = *data_ptr;
>
> count = llist::i_size();
> if ( count <= 0 )
> {
> return ( FALSE ); /* position out of range */
> }
>
> if (data_ptr == NULL)
> {
> data_ptr = LinkedList;
> return ( TRUE );
> }
>
> if ( from_where == 0 )
> {
> data_ptr = LinkedList;
> return ( TRUE );
> }
>
> if ( data_ptr->next != NULL )
> {
> data_ptr = temp_ptr->next;
> return ( TRUE );
> }
> else
> {
> return ( FALSE );
> }
> }
>
> Thanks,
>
> Steve
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads