?
Solved

About pointers & functions.

Posted on 2003-03-09
5
Medium Priority
?
157 Views
Last Modified: 2010-04-01
#include <iostream.h>
 
void exchange(int a, int b)  
{
 int c=a;
 a=b;
 b=c;
}
 
void main()
{
 int x=10, y=20;
 exchange(x,y);
 cout << "x=" << x  << "y=" << y ;
}
 
I am new at C++ and lately, I've learned the pointers. I understand them well, but I dunno when we use them. In the program above why doesn't it do value exchange ?
0
Comment
Question by:eXistenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 8097304
#include <iostream.h>
 
void exchange(int a, int b)  
{
 int c=a;
 a=b;
 b=c;
}
 
void main()
{
 int x=10, y=20;
 exchange(x,y);
 cout << "x=" << x  << "y=" << y ;
}


From your code, you are actually doing a pass by value into the exchange function. You are doing the exchanging of values in the function itself meaning to say if you do a display on variables a and b, you will see the exchange. However, once the function exits and returns to the main program, x and y still retain its original value. To do a "real" exchange, you can pass by using address like this

#include <iostream.h>
 
void exchange(int *a, int *b)  
{
 int c=*a;
 *a=*b;
 *b=c;
}
 
void main()
{
 int x=10, y=20;
 exchange(&x,&y);
 cout << "x=" << x  << "y=" << y ;
}



hongjun
0
 
LVL 11

Accepted Solution

by:
bcladd earned 150 total points
ID: 8097557
Hongjun is right as far as he goes. In C the use of pointers and the address operator is the manner of passing by reference (passing in parameters that will be changed in the function). C++ introduced reference parameters, parameters that "look" like regular parameters but really refer to the actual parameter passed in:

void exchange(int & a, int & b) // note the '&'; indicates reference
{
  int temp = a; a = b; b = temp;
}

int main()
{
  int x = 10, y = 20;
  exchange(x, y); // no need to use the address-of operator; fewer mistakes
  cout << "x = " << x << " y = " << y << endl;
}

Under the hood, reference parameters pass the address of the actual parameter (thus it has to be something that has an address, an l-value).

Hope this helps, -bcl
0
 

Author Comment

by:eXistenz
ID: 8097568
yeah, my target from this snippet was to understand why I should use pointers here. thanks hongjun, bcladd . :) I understand now .
0
 

Author Comment

by:eXistenz
ID: 8097620
Can you please explain when we have to use pointers ?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8097996
You must use pointers with _dynamically allocated memory_. That is, if you use new (or malloc) to allocate memory on the heap, that memory has no name. The only way to refer to it is by its location; a pointer holds the location of an object of the appropriate type in memory.

As parameters, you typically only need to use pointers when passing around dynamically allocated memory (or linked-lists or trees of dynamically allocated memory). When working with parameters you want to modify, you can usually get better (easier to read, easier to maintain, easier to use) results by using reference parameters.

-bcl
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question