Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

About pointers & functions.

Posted on 2003-03-09
5
Medium Priority
?
163 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

564 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