![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Jun 2005
Posts: 2
| Having trouble using the keyword new inside of a function when passing a pointer in. Code:
#include <cstdlib>
#include <iostream>
using namespace std;
struct person_details{
char name[100];
char phone[100];
};
void getDetails( person_details * pd ){
pd = new person_details[1];
strcpy( pd[ 0 ].name, "John Doe" );
}
int main(int argc, char *argv[])
{
person_details *pDetails = 0;
getDetails( pDetails );
cout << "pDetails[ 0 ].name " << pDetails[ 0 ].name << endl;
delete pDetails;
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited by klanglie; 06-15-2005 at 01:25 PM. |
| klanglie is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 5,005
| Pass the pointer by reference. Code: void getDetails( person_details * &pd ){
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #3 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| Other alternatives... #2 - Pass a pointer-to-a-pointer: Code: #include <cstdlib>
#include <iostream>
using namespace std;
struct person_details{
char name[100];
char phone[100];
};
void getDetails( person_details ** pd ){
*pd = new person_details;
strcpy( (*pd)->name, "John Doe" );
}
int main(int argc, char *argv[])
{
person_details *pDetails = 0;
getDetails( &pDetails );
cout << "pDetails->name " << pDetails->name << endl;
delete pDetails;
system("PAUSE");
return EXIT_SUCCESS;
}
Code: #include <cstdlib>
#include <iostream>
using namespace std;
struct person_details{
char name[100];
char phone[100];
};
person_details * getDetails()
{
person_details * pd = new person_details;
strcpy( pd->name, "John Doe" );
return pd;
}
int main(int argc, char *argv[])
{
person_details *pDetails = getDetails();
cout << "pDetails->name " << pDetails->name << endl;
delete pDetails;
system("PAUSE");
return EXIT_SUCCESS;
}
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #4 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| Code: int main(int argc, char *argv[]) Code: int main()
{
return 0;
}
a) What's the point of creating an array of length 1? b) It seems to me, inside a function you would use the operator 'new' to pass a pointer out of the function when the pointer variable was declared inside the function. In other words, 'new' creates a pointer with no scope limitations, and the only thing that will make the pointer go out of scope is delete. For instance, Code: #include <iostream>
using namespace std;
int* myFunc(int num)
{
int* ptr = new int(num);
return ptr;
}
int main ()
{
int* myPtr = 0;
myPtr = myFunc(10);
cout<<*myPtr<<endl;
delete myPtr;
return 0;
}
The problem with your code is that despite what you may have read, pointers are "passed by value"(in fact everything is passed by value). When you pass a pointer to a function, a copy of the pointer is made for the function, and the function uses the copy. You are seeing the consequences of that fact inside your function. Inside your function, you are using 'new' to grab some memory, and you are assigning the address of that memory to the copy of the pointer. When the function ends, the copy of the pointer is destroyed, and the original pointer in main() is left unaffected. The reason books will say that pointers allow "passing by reference" is that the object the pointer points to is "passed by reference", i.e. the object the pointer points to is not copied for the function. However, the pointer you pass to the function is copied for the function. Since a copy of a pointer still points to the same object as the original pointer, the function can use the copy of the pointer to change the original object. However, in your function, you are not trying to change the original object --you are trying to change the original pointer, which is not going to work. There is a solution if you want to stick with what you are doing. Just like with an object, you can pass a pointer by reference if you want. If you treat your pointer as an "object", how would you pass the "object" by reference? You would either use a pointer to the "object" or a reference to the "object". If you use a pointer, the pointer will be copied for the function just like before, but a copy of the pointer still points to the original "object", so you can use the copy of the pointer to change the original "object". Last edited by 7stud; 06-15-2005 at 03:37 PM. |
| 7stud is offline | |
| | #5 |
| Registered User Join Date: Jun 2005
Posts: 2
| Thanks for the quick response, guys! This information ought to help me out a great deal. I am definitely finding out that using pointers and pointers to pointers is a little confusing, but you all have helped me to clear up some issues. I will probably do what hk_mp5kpdw and 7stud suggested and return a new object instead of passing in a pointer. Also, in my original code snippet, I used delete pDetails instead of delete [] pDetails. Since I will be dynamically creating an array of person_details bigger than one dimension, I should probably be using the later. Again, thanks a lot. cprogramming.com rocks!!! Last edited by klanglie; 06-15-2005 at 08:57 PM. |
| klanglie is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Following CTools | EstateMatt | C Programming | 5 | 06-26-2008 10:10 AM |
| Passing a pointer to two-dimension array in a function | E_I_S | C++ Programming | 11 | 06-19-2008 09:57 AM |
| What is a virtual function pointer? | ting | C++ Programming | 4 | 03-05-2008 02:36 AM |
| pointer assignment inside a function ??? | gemini_shooter | C Programming | 7 | 05-04-2005 11:30 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |