Jump to content
  • Advertisement
Sign in to follow this  
slayemin

Easy question on pointers [resolved]

This topic is 3642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to figure out C++ pointers for a simple assignment but I'm stuck on something that seems like it'd be really easy: A function receives two input parameters. The first parameter is used by the function to do something and the second parameter is a pointer which gets changed. Here's a sample:
#include <iostream>
#include "ICE1.h"

using namespace std;

void func1(int i, int *j);

int main(void)
{
	int c = 5;
	int *b = NULL;

	func1(c,b);

	cout << *b;//this should be '6'

	delete b;

	return 0;
}

void func1(int i, int *j)
{
	//this should change the j ptr
	j = new int(i+1);
}


Initially I was passing in 'j' by reference and it was working just the way I wanted, but that's not what I'm supposed to do. Is there a way to pass a pointer by reference as a parameter and change where it points? [Edited by - slayemin on October 27, 2008 4:02:59 AM]

Share this post


Link to post
Share on other sites
Advertisement
When you want to make a permament change to a parameter thats passed to a function you can use a pointer.Now you want to make a permanent change to a pointer so you can use a pointer to pointer :)

Here is the code :

#include <iostream>


using namespace std;

void func1(int i, int **j);

int main(void)
{
int c = 5;
int *b = NULL;

func1(c,&b);

cout << *b;//this should be '6'

delete b;

return 0;
}

void func1(int i, int **j)
{
//this should change the j ptr
*j = new int(i+1);
}



Beware though allocating like this in a function and deleting outside maybe risky in the future when things get more complex.
I suggest looking into boost::shared_ptr early on to save headaches.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
To change an argument, you need to pass it by reference. Hence, in modern C++:
void func1(int i, int *&j)


ToohrVyk, Thank you!

I was trying it with the ampersand and asterisk swapped and ran into trouble. What a nuance :)

Again, thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by slayemin
Quote:
Original post by ToohrVyk
To change an argument, you need to pass it by reference. Hence, in modern C++:
void func1(int i, int *&j)


ToohrVyk, Thank you!

I was trying it with the ampersand and asterisk swapped and ran into trouble. What a nuance :)

Again, thanks!


Type names are read backwards: "reference to (pointer to (int))". Alternatively, 'int*&' is what you want because 'int*' is the type that you want to pass by reference with the final '&'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!