functions accepting pointers vs references

D

darren

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!
 
K

Kai-Uwe Bux

darren said:
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
 
S

sivad.be

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.......................................
 
T

thomas.mertes

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.
 
N

Nick Keighley

On 16 May, 10:08, (e-mail address removed) 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.
 
I

Ivan Vecerina

: 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top