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

functions accepting pointers vs references

Hi everybody

I am a bit confused about functions that take pointers / references as
parameters.
Are these two functions the same?

void myFunc(int& anInt);
void myFunct(int* anInt(0;

Would the difference be that, in the first function, i would us anInt
without dereferencing it, but in the second I would have to
dereference? Thanks for any clarification!

Jun 27 '08 #1
8 1158
darren <mi******@gmail.comwrites:
>Hi everybody
>I am a bit confused about functions that take pointers / references as
parameters.
See
http://www-h.eng.cam.ac.uk/help/tpl/...tml#References
(which has links to the FAQ)

Jun 27 '08 #2
darren wrote:
Hi everybody

I am a bit confused about functions that take pointers / references as
parameters.
Are these two functions the same?

void myFunc(int& anInt);
void myFunct(int* anInt(0;
You mean

void myFunc(int& anInt);
void myFunct(int* anInt);

Those are functions with different signatures.

Would the difference be that, in the first function, i would us anInt
without dereferencing it, but in the second I would have to
dereference? Thanks for any clarification!
The first function takes an int& as an argument and the second an int*.
Those are different types.
Best

Kai-Uwe Bux

Jun 27 '08 #3
hi..........this is ur answer please read the following

in c++ programming language .......first of all u have to know about
difference between & and*(operators)
&-dereferencing operator
this operator is used for assigning operand's location address value
to another
operand

*-asterisk(pointer operator)
this operator is used to point or make one variable or operand to have
address location value to that variable .........now u will be clear
and u can able to answer ur question by u
itself.......................................
Jun 27 '08 #4
On 16 Mai, 09:30, darren <minof...@gmail.comwrote:
Hi everybody

I am a bit confused about functions that take pointers / references as
parameters.
Are these two functions the same?

void myFunc(int& anInt);
void myFunct(int* anInt(0;

Would the difference be that, in the first function, i would us anInt
without dereferencing it, but in the second I would have to
dereference? Thanks for any clarification!
The two functions probably have the prototypes:

void myFunc(int& anInt);
void myFunct(int* anInt);

You see the difference when you call them:

int i;
myFunc(i);
myFunc(&i);

As you can see:
The actual parameter used for a reference can just used
as is, while the actual parameter for the pointer needs
the & operator (which returns the address of i).

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
Jun 27 '08 #5
"darren" <mi******@gmail.comwrote in message news:7f**********************************@w4g2000p rd.googlegroups.com...
Hi everybody

I am a bit confused about functions that take pointers / references as
parameters.
Are these two functions the same?

void myFunc(int& anInt);
void myFunct(int* anInt(0;

Would the difference be that, in the first function, i would us anInt
without dereferencing it, but in the second I would have to
dereference? Thanks for any clarification!
You have had some answers already.
A difference that has not yet been mentioned is that the second function
you need to check for a NULL pointer, whereas in the first one you don't.
Jun 27 '08 #6
On 16 May, 10:08, sivad...@gmail.com wrote:

Please leave the text you are replying to in your post.

Your posts will be easier to read if you use standard spelling and
puctuation.

u -you
ur -your
............ -, or .

hi..........this is ur answer please read the following

in c++ programming language .......first of all u have to know about
difference between & and*(operators)
good idea

* * * &-dereferencing operator
no

& is the address-of operator
* is the dereference operater
this operator is used for assigning operand's location address value
to another operand

* * **-asterisk(pointer operator)
this operator is used to point or make one variable or operand to have
address location value to that variable
this is gibberish. * as a unary operator returns the value held at
the address. You may be confusing the use of unary-* in a declaration
and a statement.

int n = 0; // n is an int
int* p; // p is a ptr-to-int

p = &n; // assign the address of n to p

std::cout << *p; // dereference p and print the value
// ie. print the value of n

.........now u will be clear
and u can able to answer ur question by u
itself.......................................

I very much doubt the OP was any clearer.
--
Nick Keighley
Jun 27 '08 #7
Tim Love wrote:
darren <mi******@gmail.comwrites:
>Hi everybody
>I am a bit confused about functions that take pointers / references as
parameters.

See
http://www-h.eng.cam.ac.uk/help/tpl/...tml#References
(which has links to the FAQ)
I actually think they do the same thing... like the web page said:
"The general rule is to use references rather than pointers unless
there's no alternative", because the reference make things more clear.
Jun 27 '08 #8
"darren" <mi******@gmail.comwrote in message
news:7f**********************************@w4g2000p rd.googlegroups.com...
: Hi everybody
:
: I am a bit confused about functions that take pointers / references as
: parameters.
: Are these two functions the same?
:
: void myFunc(int& anInt);
: void myFunct(int* anInt(0;
:
: Would the difference be that, in the first function, i would us anInt
: without dereferencing it, but in the second I would have to
: dereference? Thanks for any clarification!

void myFunc(int& anInt);
This function receives a single integer variable as a parameter.
A valid value must always be passed, which the function may
read from or write to.

void myFunc(int* anInt);
This function takes a pointer to a (modifiable) integer; or maybe
to some position within an array of integers.
It might receive a Null pointer as an indication that the parameter
is not available.
I hope these descriptions make it clear that these two ways of
passing parameters are not interchangeable.

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
9
by: Rahul | last post by:
Hi Everyone, I was wondering about references to functions, so i tried this, int (& p) (); // doesn't work as array of references is illegal as memory is not allocated for references,...
0
by: Irfy | last post by:
Could someone elaborate on the purpose and concrete usage scenarios of references to functions. What can references to functions do that pointers to functions cannot. Why are they in C++? A swap...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.