473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointer Swap local vs. global func, whats the difference?

Hi,

does anyone know what the difference is between:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout; 
  4. using std::endl;
  5.  
  6. void func( int *p1, int* p2 )
  7. {
  8.     p2 = p1;
  9. }
  10.  
  11. int main(int argc, char* argv[]) {
  12.     int* p2;
  13.     int* p1 = new int;
  14.  
  15.     func(p1, p2);
  16.  
  17.     cout << "P1 Address: " << p1 << " P1 Value: " << *p1 << endl;
  18.     cout << "P2 Address: " << p2 << " P2 Value: " << *p2 << endl;
  19.  
  20.     return 0;
  21. }
  22.  
and

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cout; 
  4. using std::endl;
  5.  
  6. void func( int *p1, int* p2 )
  7. {
  8.     p2 = p1;
  9. }
  10.  
  11. int main(int argc, char* argv[]) {
  12.     int* p2;
  13.     int* p1 = new int;
  14.  
  15.     p2 = p1;
  16.  
  17.     cout << "P1 Address: " << p1 << " P1 Value: " << *p1 << endl;
  18.     cout << "P2 Address: " << p2 << " P2 Value: " << *p2 << endl;
  19.  
  20.     return 0;
  21. }
  22.  
I just wondered because i got different output.

output first:
P1 Address: 0x804b008 P1 Value: 0
P2 Address: 0x804b008 P2 Value: 0

output second:
P1 Address: 0x804b008 P1 Value: 0
P2 Address: 0xbfb680f8 P2 Value: -1078558376

Obviously its not the same address and therefore not the same value, but can someone explain why?

Thanks,
amonsch

P.S.
I use g++ (SUSE Linux) 4.3.2 [gcc-4_3-branch revision 141291]
with openSuse 11.1 on a 32-bit maschine
Aug 6 '09 #1
1 1813
mac11
256 100+
Firstly I think your outputs are swapped, meaning I think the first program (the one that uses func()) produced the second output.

When you call func() it gets copies of your pointers p1 and p2 and cannot change the actual p1 and p2. Simply put, the assignment inside func() does nothing to the p1 and p2 from main.

When you use assignment (p2 = p1) you're making p2 point to the same place as p1.

I've extended and modified your concept program to demonstrate further

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void func( int *p1, int* p2 )
  6. {
  7.     p2 = p1;
  8. }
  9.  
  10. void ptr_func(int **p1, int **p2)
  11. {
  12.     *p2 = *p1;
  13. }
  14.  
  15. int main(int argc, char* argv[]) 
  16. {
  17.     int* p2;
  18.     int* p1 = new int(3);
  19.  
  20.     cout << "--- before doing anything ---" << endl;
  21.     cout << "p1 @" <<  &p1 << " -> " << p1 << " : " << *p1 << endl;
  22.     cout << "p2 @" <<  &p2 << " -> " << p2 << " : " << *p2 << endl;
  23.     cout << endl;
  24.  
  25.     func(p1, p2);
  26.  
  27.     cout << "--- after using func() ---" << endl;
  28.     cout << "p1 @" <<  &p1 << " -> " << p1 << " : " << *p1 << endl;
  29.     cout << "p2 @" <<  &p2 << " -> " << p2 << " : " << *p2 << endl;
  30.     cout << endl;
  31.  
  32.     p2 = p1;
  33.  
  34.     cout << "--- after using direct assignment ---" << endl;
  35.     cout << "p1 @" <<  &p1 << " -> " << p1 << " : " << *p1 << endl;
  36.     cout << "p2 @" <<  &p2 << " -> " << p2 << " : " << *p2 << endl;
  37.     cout << endl;
  38.  
  39.  
  40.     ptr_func(&p1, &p2);
  41.  
  42.     cout << "--- after using ptr_func() ---" << endl;
  43.     cout << "p1 @" <<  &p1 << " -> " << p1 << " : " << *p1 << endl;
  44.     cout << "p2 @" <<  &p2 << " -> " << p2 << " : " << *p2 << endl;
  45.     cout << endl;
  46.     return 0;
  47. }
  48.  
My output is as follows:
--- before doing anything ---
p1 @0xbff473b0 -> 0x9571008 : 3
p2 @0xbff473b4 -> 0x599ca0 : 0

--- after using func() ---
p1 @0xbff473b0 -> 0x9571008 : 3
p2 @0xbff473b4 -> 0x599ca0 : 0

--- after using direct assignment ---
p1 @0xbff473b0 -> 0x9571008 : 3
p2 @0xbff473b4 -> 0x9571008 : 3

--- after using ptr_func() ---
p1 @0xbff473b0 -> 0x9571008 : 3
p2 @0xbff473b4 -> 0x9571008 : 3


Hopefully this helps you understand pointers and function parameters. Notice that ptr_func() takes the address of p1 and p2, this means that the assignment within ptr_func() actually does modify the p1 and p2 from main.
Aug 6 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
11
by: Harry | last post by:
Hi all, I am unable to understand the difference in the following codes: First Example void func(char **ch) { char *ptr; ptr=new char;
2
by: lou zion | last post by:
hi all, i've got a class that takes a parameterless function pointer as a parameter. i want to store that function pointer in a variable and i'm trying to figure out the syntax. i came up with...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: bilaribilari | last post by:
Hi, I came across a function which looks like this: void func ( Object *& param1 ) { .... } Can someone please explain what does *& mean? Where can I get a good understanding of 'pointer...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
3
by: Ciur Eugen | last post by:
HI coders ! Here is a code well commented snippet : void stop( PROCESS_INFORMATION* p ) // enters in "stop" with "p == NULL" { static PROCESS_INFORMATION pi; p = &pi; //now p !=...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.