473,387 Members | 1,611 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,387 software developers and data experts.

Arguments as pointer or address?

MrPickle
100 100+
I am unclear on when an argument should be a pointer or an address? Aren't they the same, just the first is pointing to the address and the second is directly giving the address?

Expand|Select|Wrap|Line Numbers
  1. void foo(int* bar); //Pointer
  2. void foo(int& bar); //Address?
  3.  
Oct 18 '08 #1
3 1259
Ganon11
3,652 Expert 2GB
Yes and no. They are both the same in the sense that, if I call one of your functions in main() with the variable myInt, any changes taking place in foo() will effect myInt, not just a copy. However, the way the functions deal with it (or rather, the way you as a programmer deal with the variable) are different.

In void foo(int *myInt) (using a pointer), you have a pointer to an int. That is, it's a pointer - you can dereference it, treat it as an array, tell it to point to a new variable, etc.

Your changes to myInt (that is, the pointer) are not reflected when foo() returns/finishes. Your changes to (*myInt) (that is, the integer pointed to by myInt) WILL be reflected when foo() returns/finishes.

In void foo(int &myInt) (using pass-by-reference), myInt is an int, NOT a pointer. You can't dereference it, or treat it as an array, or whatever.

Any changes you make to myInt (again, it's an int) are reflected when foo() returns/finishes.

So basically, when programming, you get a pointer when passing pointers, and you get an int when passing-by-reference.

(Internally, you're actually passing pointers in both situations, but with pass-by-reference, the computer takes a few extra steps for you so that you, the coder, can treat it as an integer).
Oct 18 '08 #2
boxfish
469 Expert 256MB
If you were to pass bar by value (no & or *), a copy of bar would be created. For larger bars, such as class types, this is a waste of time and space, but it means that the function cannot modify the original bar.
If you pass bar by pointer (with *), you have the benifit of not making a copy of bar (only the pointer gets copied), but your function will have to deal with awkward pointer syntax. Also, the function can modify the original bar.
Passing by reference (with &) is supposed to be the best of both worlds. You can't do it in C, only C++. You actually pass a pointer to bar, but it's behind the scenes. You use bar in your function just like you would if you had passed it by value, but it's not a copy of bar, so if you modify it, the original bar gets modified.
If you want to pass by reference to avoid copying bar, but want to ensure that your function doesn't modify bar, pass by constant reference, with const and &.
Passing by constant reference instead of passing by value saves time and space with objects that take up a lot of memory, but with little things like ints, just pass by value.
Expand|Select|Wrap|Line Numbers
  1. void foo(int bar); // By value
  2. void foo(int* bar); // By pointer
  3. void foo(int& bar); // By reference
  4. void foo(const int& bar); // By constant reference.
  5.  
Oct 18 '08 #3
MrPickle
100 100+
Okay, I think I get it now, thanks!
Oct 18 '08 #4

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

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
4
by: Jonathan Green | last post by:
I have some questions about the behavior of the code below. #include <stdio.h> void test(void *v); int main(void) { int i=1; int j=2;
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.