This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
I so confused about parameter types in C++ I have two functions in following code:
void degistir2( int *x, int *y )
{
int gecici;
gecici = *x; *x = *y;
*y = gecici;
}
void degistir3( int &x, int &y )
{
int gecici;
gecici = x; x = y;
y = gecici;
}
What is the difference of these functions? I know the pointers and references but I don't know how it works in above functions.
In your first example, your function is given a copy of the address of x and y.
In the second example, your function is given the same instance of x and y as the code that function call originates from.
By default, functions receive a copy of the variable being passed to the function. Both of your examples allow you to access the original x and y by different methods.
See 7.2-7.4 of this guide for more details and examples.
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:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 3 years ago.
I am learning about programming in C++ specifically dealing with pointers. In the code below, I don't understand why doub(int x) does not change the value of y while both trip(int* x) and quint(int& x) do. I understand that the last two functions both take in the address of the object while first takes in the object. Any explanation or links to resources is greatly appreciated thanks!
#include <iostream>
using namespace std;
void doub(int x) {
x = x * 2;
}
void trip(int* x) {
*x = *x * 3;
}
void quint(int& x) {
x = x * 5;
}
int main() {
int y = 7;
doub(y);
cout << y << endl; // prints 7 why does this not print 14?
trip(&y);
cout << y << endl; // prints 21
quint(y);
cout << y << endl; // prints 105
}
void doub(int& x) {
x = x * 2;
}
would change the value of y in main. This happens because int x makes a copy of the integer, while int& x passes it by reference (something like a pointer, kinda).
Edit to explain references a bit more: Actually, the closer you can get right now in understanding is saying that when you are passing by reference, then x in int& x will be another name for your y variable in main, or in other words, an alias. On the other hand, int x creates a temporary integer that is a copy of the original.
That will print 7 because it's the no pointer case. The value got copied to the stack on function call and popped off on function return.
To make it change is why we have pointers. To make it less ugly is why we have references.
This question already has answers here:
size of reference variable in c++ [closed]
(1 answer)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
Closed 4 years ago.
Consider the following program:
#include <stdio.h>
struct A
{
int x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
As expected, this program prints the number 4 because the integer in A uses 4 bytes. However, let's change the program into this:
#include <stdio.h>
struct A
{
int x;
int& r = x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
Now the program prints the number 16.
I've always been told that references in C++ works like an alias, as in offering another name for the same variable. Thus, I expected the struct to still have a size of 4 bytes since the reference r is just another name for x, which will get replaced by the actual variable x when compiled.
Obviusly, this doesn't seem to be the case. Instead, it seems that the reference takes up just as much memory as a pointer would do. If we change int& r = x; into int* p = &x; the program will also print 16. From my point of view this doesn't make any sense because why have references if they take up memory just as pointers do, then we might just use pointers instead.
I did one more test program with the following code:
#include <stdio.h>
struct A
{
int x;
int y;
int z;
int& X = x;
int& Y = y;
int& Z = z;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
And the program above prints 40. That seems very weird in my eyes since the 3 integers should take up 4 bytes each, making it 12 bytes in total, and leaving 28 bytes left for the references. But 28 is not evenly divisible by 3.
I've compiled this with Visual Studio 2015 in x64 configuration.
So my question is: Have I done something wrong here or why does a C++ reference use up memory in a struct?
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