C++ - Pointers, Call by reference, Basic

I have a long-term fear of pointers and I am trying to start from the beginning to make my roots strong. As basic as this question may seem, can someone address this query? Would be of great help. I am basically trying to pass by reference. Why doesn’t the function myf2 work?. The output is coming as:

Values: d=0, y=25
Values: d=25, y=25

CODE:

#include<iostream>
using namespace std;

void myf2(int *x, int *y)
{
	x=y;
}

int* myf3(int *y)
{
	return y;
}

int main()
{
	int *d, a=25;
	d = new int;
	int *y = new int;
	y=&a;
	myf2(d,y);
	cout<<"Values: d="<<*d<<", y="<<*y<<endl;
	d=myf3(y);
	cout<<"Values: d="<<*d<<", y="<<*y<<endl;
   return 0;
}

The problem with the code is that in the first function you are passing by reference, hence in the function myf2 int *x and int *y, x and y are local variables of that function which point to the same integers as pointed by d and y in the main function. Hence just doing pointer manipulations that is assigning one pointer to another would not do anything. Hence you need to swap or manipulate the things pointed by these pointers in order to make a real change in the variables in the main function.

There’s a difference between passing by reference and “passing by pointer”…

Remember that a pointer is simply a variable which stores the address of a variable, and then, the possible manipulations over pointers are:

    int main()
{
    int *y = new int;
    int x = 20;
    int z = 3;
    cout << "Address of y is: " << y << endl;
    cout << "Value of variable at address of y is: " << *y << endl;
    *y = z;
    cout << "Now value of y is: " << *y << endl;
    int *val = &z; //val has the value referenced by the memory address z: 3
    cout << *val << endl;
   return 0;
}

Try to play with this a bit and try to understand how to write a swap function…

That might help you

Best,

Bruno

Hello @s1d_3,

I’m quite busy with univ, but, if you’re using C++ to accomplish your task, and if you’d like to chat about it, maybe you can e-mail me at olivbruno8 at gmail dot com and I can reply whenever I have time, even if it’s just to discuss small examples.

What I meant is that you can use “pass by reference” and “pass by pointer” to swap two values for example.

They both do the same thing, but, they use different notations:

#include <iostream>
using namespace std;

//pass by reference
void swap(int & x, int & y)
{
	int tmp;
	tmp = y;
	y=x;
	x=tmp;
}

//pass by pointer
void swap2(int *x, int *y)
{
	int tmp;
	tmp = *y;
	*y = *x;
	*x = tmp;
}

int main()
{
    int x = 4;
    int y = 1;
    int z = 7;
    int t = 2;
    swap(x,y);
    swap2(&z,&t);
    cout << x << " " << y << endl;
    cout << z << " " << t << endl;
    return 0;
}

On the swap procedure, you pass as parameters to it the references of the values which are stored at the locations pointed by x and y, which means that the pointers are “transparent” to the function and you don’t need to use the * symbol.

When the references get swapped, the identifiers remain the same, x identifies x and y identifies y, but the values are swapped, as intended.

On the swap2 procedure however, we pass as parameters to it, the actual memory addresses where the values are located, and we swap the memory addresses where they are located, so the values also change.

Since values in memory are referenced by their memory locations, the 2 above procedures are the same,as swapping references to values or swapping memory locations is basically the same thing (I hope I’m saying right things here…).

The only significant difference is the syntax used, as imo, the 1st one is more clear if you are aware of the power and meaning of the & symbol.

Best,

Bruno

So does the 9 people who saw this just doesn’t care? Or do they have problems with pointers too? One even bothered to edit my question, for trifle details. Or I am just plainly missing something.
Sorry for the aggression. I am stuck for quite some days with pointers and I messed up a very important interview very badly. Would love to get some help ASAP.

aaahh thanx that was good explanation! So instead of x=y, (star)x=(star)y worked

(the asterisk mark isn’t getting recognized here :stuck_out_tongue: )

Hi Bruno,

Appreciate the effort. Actually I have a good basic understanding of what a pointer is and about the difference between locations and addresses. I just get confused whenever I have to pass them to functions.

I was actually implementing a Binary Search Tree and was solving a simple problem about converting a BST to a Doubly-Linked-List in Sorted Order. So while passing the head and tail of the LL, there are some issues. Its a quite lengthy but simple code. Do you wanna have a look at it? It would be really helpful once I get this clear I guess. Can I attach a doc or something here?

Also, can you just write the function declaration to prove how passing by reference is different from passing by pointer?

I had the understanding that if I pass a pointer, I am basically passing a reference to it so all changes done to it will be reflected to my original values?

There are several users around here. Someone gonna help you for sure. It’s not our forced duty to answer each and every question we see. And if you doubt anybody’s intellect then it’s not gonna help you either. Also, before asking a question you should search for the similar questions discussed here. You were stuck for 2 days but didn’t ask the same 2 days ago. Just have patience!

Hi Bruno. First up, thanks a ton for putting up so much effort for a stranger. I am extremely grateful.

Yes I do use C++. I just mailed you with the particulars and the code. Im going through this snippet you sent now.

Apologies for the delay in response. I am from India actually. Timezones :stuck_out_tongue: