1 Replies - 2123 Views - Last Post: 26 November 2009 - 11:40 PM Rate Topic: -----

#1 Trekforever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-November 09

Passing pointer from one function to another

Posted 26 November 2009 - 11:06 PM

Is it possible to pass a pointer from one function to the main function without using return? For example, suppose I want to find the address of the maximum value in an array.
void findMax(int arr[], int n, int* pToMax);
int main()
	{
		int nums[4] = { 5, 3, 15, 6 }; 
		int *ptr = nums;

		findMax(nums, 4, ptr);
		cout << "The maximum is at address " << ptr << endl;
	}

void findMax(int arr[], int n, int* pToMax)
	{
	int max = arr[0];
	pToMax = &arr[0];
	
	for (int i = 1; i < n; i++)
	{
		if (arr[i] > max)
		{
			 max = arr[i];
			 pToMax = (arr+i);
		}
	}
	}  


So the findMax function finds the address of the maximum value and stores it to pToMax. Is there any way on passing that into the main function without changing the return type?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Passing pointer from one function to another

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Passing pointer from one function to another

Posted 26 November 2009 - 11:40 PM

Yes, you pass your pointer to the function and accept it as a Reference to a pointer. Just like normal references, but the value is a pointer. Then by altering this pointer you are altering the original pointer back in calling function. You will see this as an asterisk followed by an ampersand. Below is the same program but one with a regular pointer and one with a reference to a pointer.

The first instance, with the normal pointer will show back in main that the value of "val" is the same address before and after the function is called.

#include<iostream>
using namespace std;

void myfunc(int *p);

int main() {
	int a = 1;
	int *val = &a;

	cout << "Address in main before function: " << val << endl;

	myfunc(val);

	cout << "Address in main after function: " << val << endl;
	
	return 0;
}

void myfunc(int *p) {

	cout << "Address in function before reassignment: " << p << endl;
	int b = 4;

	p = &b;
	cout << "Address in function after reassignment: " << p << endl;

}



Now we will use a reference pointer which we will see that the address stored in val is changed after we get back from the function. Just like when you pass any value by reference...

#include<iostream>
using namespace std;

void myfunc(int *&p);

int main() {
	int a = 1;
	int *val = &a;

	cout << "Address in main before function: " << val << endl;

	myfunc(val);

	cout << "Address in main after function: " << val << endl;
	
	return 0;
}

void myfunc(int *&p) {

	cout << "Address in function before reassignment: " << p << endl;
	int b = 4;

	p = &b;
	cout << "Address in function after reassignment: " << p << endl;

}



So using this mechanism we can alter values passed by reference when using pointers just like when we pass any value as a reference. You will typically see this method used in algorithms dealing with traversing binary trees and other data structures dealing with nodes for instance.

Hope this is what you were talking about. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1