By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Ampersand and Pointer

P: 1
Hello,

I want to know the difference between the following two?

void some_func(int &a);

void some_func(int *a);

Thank you.
Jun 15 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 181
Hello,

I want to know the difference between the following two?

void some_func(int &a);

void some_func(int *a);

Thank you.
In first a is declared as alias. Latter in function u cannot assign a to some other value

In some_func(int *a) a is pointer to integer. if u want u can assign a to some other value

a = &b; is allowed here
Jun 15 '07 #2

P: 9
int &a; // its a alias.

int *b; its a pointer ..


example :

case 1: // referance..

int b = 10;

int &a = b;

b = 15;

a = 16;

cout << a << b; \\ both have the same value a= b =16.

this is known as alias...

func( int &a, int &b)
{

a++;
b ++;
}

main()
{

int a = 1;
int b = 2;

func(a,b);


cout << a << b; // o/p 2 3
}

case 2: // pointer

in this case

int a = 10;

int *b = &a; // this take means, b as the address of variable a, to get the value of a, you have to use dereferencing operator.

example:

func( int *a, int *b)
{

*a++; //a ++ , if you do this the address will be incremented., so use dereferencing operator (*)
*b ++;
}

main()
{

int a = 1;
int b = 2;

func(&a,&b);


cout << a << b; // o/p 2 3
}

----------

so comparing to pointers reference variable is more easy.
Jun 15 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Please use code tags in your postings.
Jun 15 '07 #4

Post your reply

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