This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
difference between a pointer and reference parameter?
Using C++ i'm wondering what's the difference in the use of & and * in parameters?
For example:
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
That apparently would swap the integers a and b. But wouldn't the following function do exactly the same?
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
I was just wondering when it is appropriate to use each one, and perhaps the advantages of each one.
The difference between pointers and references is that pointers can point to "nothing", while references cannot. Your second sample should null-check pointers before dereferencing them; there is no need to do so with references.
Related
This question already has answers here:
Are there benefits of passing by pointer over passing by reference in C++?
(7 answers)
Pass by reference vs pass by pointer? [duplicate]
(3 answers)
When pass-by-pointer is preferred to pass-by-reference in C++?
(13 answers)
Performance cost of passing by value vs. by reference or by pointer?
(6 answers)
How to pass objects to functions in C++?
(8 answers)
Closed 6 months ago.
I want to have a explanation with respect to variable created on stack memory, what is the difference between pointer and reference. I understand in the case of calling by pointer, for example, the following things are happening.
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main(){
int i1 = 3, i2 = 7;
swap (&i1,&i2);
return 0;
}
Two variables i1, i2 are created on stack framed to the main function.
at calling 'swap' function, two more variables x, y, are created on stack, framed to the 'swap function. These two variables are initialized at the address of i1 and i2.
a temp variable is created on stack framed to 'swap' function. All of x,y,temp disappear after 'swap'.
Could someone explain in a similar way what happens when I use 'call by reference'? In particular, is there a variable x and y created on stack when calling 'swap'? If so, what are they?
#include <iostream>
using namespace std;
void swap(int &x, int &y){
int temp;
temp = x;
x = y;
y = temp;
}
int main(){
int i1 = 3, i2 = 7;
swap (i1,i2);
return 0;
}
Thank you!
you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the address itself. Sometimes you will need to modify the address (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the address, reference won't
This question already has answers here:
Pointer vs. Reference
(12 answers)
Closed 6 years ago.
I found these two different sources, but they do the exact same thing. I was wondering if there is a difference or not but I couldn't figure it out.
Can anyone tell me the difference and when should I use which?
this is the first one:
void function1(int *x) {
*x = 100;
}
int main() {
int var1 = 10;
function1(&var1);
cout << var1 << endl;
}
and this is the second one:
void function2(int &x) {
x = 100;
}
int main() {
int var2 = 10;
function2(var2);
cout << var2 << endl;
}
int *x is a pointer whereas int &x is a reference. Probably the biggest difference is that you can't change where reference is pointing to.
The first is a pointer, the second is a reference. The ideas have some similarities, but there are also differences.
A pointer is a C and C++ mechanism and a bit more "pure" but gives you more posibilies for advanced concepts like pointer arithmetics. References are C++ only and are more safe and more implicit, as a reference is used with the same syntax as a normal varible while using the referenced one. A pointer is more explicit if you want to use or change its value, as you have to explicitely dereference it using *var, and explicitely have obtain it.
This question already has answers here:
what does this mean char (*(*a[4])())[5]?
(4 answers)
Closed 6 years ago.
so I've been studying pointers, trying to understand them.
I know that in the following line
int f(int ni, int n);
f is a function that accepts two int variables as its input and it returns an int as the result
If I write the following line
int (*f)(int ni, int n);
then f is a function pointer
However, what happens when I write something like?
int (*f[4])(int p);
Thanks for your help.
This is an array of 4 pointers to function, example:
int foo(int p) {
return 0;
}
int (*f[4])(int p);
f[0] = foo;
f[1] = foo;
f[2] = foo;
f[3] = foo;
refer to this link :
Array functions pointer
There's explainations about what is does and how to implement it
This question already has answers here:
Uses for multiple levels of pointer dereferences?
(17 answers)
Pointer to a pointer to a pointer [duplicate]
(4 answers)
Closed 8 years ago.
For example:
bool insertInFront( IntElement **head, int data ){
IntElement *newElem = new IntElement;
if ( !newElem ) return false;
newElen->data = data;
*head = newElem; // Correctly updates head
return true;
}
I am new to C++, coming from Java. I get the * for indirection syntax, but ** is not listed on this page: http://en.wikipedia.org/wiki/Operators_in_C_and_C++#Member_and_pointer_operators
I found this example on page 28 of Programming Interviews Exposed
Update
I realize that this question is naive, and I probably could have found an answer through other means. Obviously, I am new to the language. Still, asking "What does ** mean?" is not well supported online for someone who does not know that ** is a pointer operation. There are very few relevant results when searching C ** syntax or C++ ** meaning. Additionally, using ctrl + f to search ** in the wiki page above, and other documentation, doesn't return any matches at all.
I just wanted to clarify, from a beginner's perspective, that this question is hard to distinguish from the duplicates. Of course, the answer is the same :-) Thank you for the help.
There is no specific ** operator in C++, instead it's two separate asterisks, and asterisks in a declaration denotes pointer declaration.
So in the declaration
IntElement **head
the argument head is declared to be a pointer to a pointer to IntElement.
Its meaning:
int a; // integer
int *ptrA = &a // pointer to a integer
int **PtrPtrA = &ptrA // point to pointer to a integer
How can it be used:
void function_nochange(int *pA ) { pA = &b; }
void function_change (int **ppA) { *ppA = &b; }
int a;
int b;
void test()
{
int *ptrA = &a
function_nochange(ptrA)
// here ptrA still point to int a since ptrA was copied
function_change(&ptrA)
// here ptrA point to int b since ptrA was passed as pointer
}
**VariableName means pointer to pointer(a chain of pointers) in C++
You can find good tutorials here :
http://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm
http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer
This question already has answers here:
What does *& mean in a function parameter
(5 answers)
Closed 10 years ago.
I found this in a final exam:
int a = 564;
int* pa = &a;
int *& pr = pa;
cout << *pr;
According to the multiple choice answer, the code is valid, and displays the value of a.
But I'm confused about evaluation and precedence for line 3. Order of operations for C states that * and & have the same order. So, would it then be int *(&pr)? How can this be described in words?
Thank you.
The third line defines a pointer reference (or a reference to a pointer, if you want). Assigning it to a pointer makes pr to actually be an alias to pa, and when evaluated, it points where pa points to, that is, a.
In the declaration of a variable, * and & don't have the meaning of operators, so precedence doesn't make sense here.
It's a reference to a pointer. In C, you would express that as a pointer to a pointer.
You could write something like this:
// C++ style
void update_my_ptr(int*& ptr) { ptr = new int[1024]; }
// C style
void update_my_ptr_c(int **ptr) { *ptr = malloc(1024 * sizeof(int)); }
int main()
{
int *ptr;
update_my_ptr(ptr);
// Here ptr is allocated!
}
Line three creates a reference (read: alias) of a pointer to an int. If you were to set pr to 0, pa would also be equal to 0 (and vice-versa).