GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-May-2006, 14:51
Esam Esam is offline
New Member
 
Join Date: Mar 2006
Posts: 3
Esam is on a distinguished road

Pointer Problem


I'm sure this is really simple and stupid, but I'm not exactly sure what I am doing wrong. I have an array in an object. I would like to accept a pointer as an argument and set that pointer to the address of some index in that array for the calling class to use. Basically, the pointer is always equal to 0 when I try to use it. I probably posted more than necessary but it is very simple so here is a test example that exhibits the problem. The main and setptr functions should make it obvious what I am trying to do.
CPP / C++ / C Code:
#include <iostream>
using namespace std;

class ptr_test
{
public:
   ptr_test();
   ~ptr_test();
   
   void setptr(int * ptr);

private:
   int * testptr;
};

ptr_test::ptr_test()
{
   testptr = new int[20];
}

ptr_test::~ptr_test()
{
   if (testptr)
   {   
      delete [] testptr;
      testptr = 0;
   }
}

void ptr_test::setptr(int * ptr)
{ 
   ptr = testptr;
   cout << "Member: " << testptr << endl;
   cout << "Argument: " << ptr << endl;
}

int main(int argc, char *argv[])
{
   ptr_test p;
   int * int_ptr;
   int_ptr = 0;
   cout << "Initial: " << int_ptr << endl;
   p.setptr(int_ptr);
   cout << "Set: " << int_ptr << endl;    
}

The result is as follows:
Initial: 0
Member: 0x3d3ca8
Argument: 0x3d3ca8
Set: 0

It is like it goes out of scope or something but I don't think it should. Any answers?

Thanks
  #2  
Old 22-May-2006, 15:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,619
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Pointer Problem


Quote:
Originally Posted by Esam
but I'm not exactly sure what I am doing wrong.

If you want a function that changes the value of one of its arguments (so that the change is reflected back into the calling program), there are a couple of ways:

1. Make the argument a pointer to the variable, then in the function de-reference the pointer to access the value of the variable (this works for C and C++). (So, if the variable is a pointer (int *arg), you would pass a pointer to the pointer (int **arg).) Syntax in the calling program changes to use the address of the pointer. Syntax in the function changes to dereference the pointer-to-pointer.

2. Make the argument a reference to the variable, then whatever you do in the function is actually acting on the original (this works for C++ only).

(Otherwise the function only changes the value of its local copy of the argument, and this does not change the value of the corresponding variable in the calling program.)

For C++, many people prefer the second one:

CPP / C++ / C Code:
   void setptr(int * &ptr);// ptr is a reference to a variable (the variable is a pointer to int)
.
.
.
void ptr_test::setptr(int * &ptr)
{ 
   ptr = testptr;
   cout << "Member: " << testptr << endl;
   cout << "Argument: " << ptr << endl;
}
.
.
.
   p.setptr(int_ptr);
.
.
.

With these changes (and only these changes), I saw the following output:

Code:
Initial: 0 Member: 0x6a2268 Argument: 0x6a2268 Set: 0x6a2268

Regards,

Dave
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome CPP / C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
Pointer Problem nusstu C Programming Language 4 15-Apr-2005 11:31
Please help! Newbie pointer problem! robsmith C Programming Language 5 12-Mar-2005 04:48
C Pointer problem fwongmc C Programming Language 8 04-Dec-2004 12:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.