🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #71

Re: Losing my pointer

#71
From: FILEUX Laurent
Date: Thu, 24 Feb 2000 00:00
81 lines
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
Technical Details
Message-ID: <38B56B8A.6ECA99D2@fr.origin-it.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <88q96p$d3f$1@bob.news.rcn.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d4ba,cce8f20ebac583c6
X-Google-Attributes: gid10d4ba,public
From: FILEUX Laurent <Laurent.Fileux@fr.origin-it.com>
Subject: Re: Losing my pointer
Date: 2000/02/24
Message-ID: <38B56B8A.6ECA99D2@fr.origin-it.com>
X-Deja-AN: 589388198
Content-Transfer-Encoding: 7bit
References: <88q96p$d3f$1@bob.news.rcn.net>
To: New C Programmer <s_joanis@siebe-ene.com>
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@nl.uu.net
X-Trace: porthos.nl.uu.net 951413665 27906 212.153.190.2 (24 Feb 2000 17:34:25 GMT)
Organization: UUNET-NL (http://www.nl.uu.net)
Mime-Version: 1.0
NNTP-Posting-Date: 24 Feb 2000 17:34:25 GMT
Newsgroups: att.lang.c++