473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help: How to pass pointers?

Hi -- I'm just starting to learn about pointers and I'm stuck on something.
The code that I pasted below passes a widget object to a function that
changes it to a new widget object. It changes it successfully within the
changer() function but once execution returns to main(), the "w" variable
still stores to the original widget. How can I change this code so that "w"
stores the new widget?

Thanks for your help,

P.S. VS.NET 2003.

Bill
#include <iostream>
using namespace std;
class widget
{
public:
int info;
};
void changer (int i, widget *x)
{
cout << i << "\n";

x = new widget;
(*x).info = i;

cout << (*x).info << "\n";
}
void main()
{
widget *w;

w = new widget;
(*w).info = 12;

cout << (*w).info << "\n";
changer (10, w);
cout << (*w).info << "\n"; //I want this to output 10 not 12.

system("PAUSE");
}
Mar 4 '07 #1
3 1104

"Bill" <Bi**@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
Hi -- I'm just starting to learn about pointers and I'm stuck on
something.
The code that I pasted below passes a widget object to a function that
changes it to a new widget object. It changes it successfully within the
changer() function but once execution returns to main(), the "w" variable
still stores to the original widget. How can I change this code so that
"w"
stores the new widget?

Thanks for your help,

P.S. VS.NET 2003.

Bill
#include <iostream>
using namespace std;
class widget
{
public:
int info;
};
void changer (int i, widget *x)
{
cout << i << "\n";

x = new widget;
(*x).info = i;

cout << (*x).info << "\n";
}
void main()
{
widget *w;

w = new widget;
(*w).info = 12;

cout << (*w).info << "\n";
changer (10, w);
cout << (*w).info << "\n"; //I want this to output 10 not 12.

system("PAUSE");
}

If you want to access the new Widget in main() you need to change the
definition of your changer() function to return a pointer to widget.
Something like this:

widget* changer (int i, widget *x)
{
cout << i << "\n";

x = new widget;
(*x).info = i;

cout << (*x).info << "\n";
return x;
}

Then change your call to: w = changer(10, w);
Mar 4 '07 #2
The whole point of passing the pointer is that changer() have a widget to work
with. Changer shouldn't be creating a new widget (you mentioned no reason to do
so). Inside changer(), *x is a pointer to the existing widget (which I think is
exactly what you want it to be). Try this:

void changer (int i, widget *x)
{
cout << "Before: " << (*x).info << "\n";
(*x).info = i;
cout << "After: " << (*x).info << "\n";
}

Perhaps you are unaware of a slightly more efficient notation for referencing a
member given a pointer:

void changer (int i, widget *x)
{
cout << x->info << "\n";
x->info = i;
cout << x->info << "\n";
}

Output:

12
12
10
10

On Sun, 4 Mar 2007 05:49:13 -0800, Bill <Bi**@discussions.microsoft.comwrote:
>class widget
{
public:
int info;
};
void changer (int i, widget *x)
{
cout << i << "\n";

x = new widget;
(*x).info = i;

cout << (*x).info << "\n";
}
void main()
{
widget *w;

w = new widget;
(*w).info = 12;

cout << (*w).info << "\n";
changer (10, w);
cout << (*w).info << "\n"; //I want this to output 10 not 12.

system("PAUSE");
}
--
- Vince
Mar 4 '07 #3
Bill wrote:
Hi -- I'm just starting to learn about pointers and I'm stuck on something.
The code that I pasted below passes a widget object to a function that
changes it to a new widget object. It changes it successfully within the
changer() function but once execution returns to main(), the "w" variable
still stores to the original widget. How can I change this code so that "w"
stores the new widget?

Thanks for your help,

P.S. VS.NET 2003.

Bill
#include <iostream>
using namespace std;
class widget
{
public:
int info;
};
void changer (int i, widget *x)
{
cout << i << "\n";

x = new widget;
(*x).info = i;

cout << (*x).info << "\n";
}
void main()
{
widget *w;

w = new widget;
(*w).info = 12;

cout << (*w).info << "\n";
changer (10, w);
cout << (*w).info << "\n"; //I want this to output 10 not 12.

system("PAUSE");
}

Bill:

1. You have two new's in your code without any delete's, so you will
leak memory.

2. Worse, the new in changer() is what stops your code form working.

3. Use the -operator.

4. It's int main() not void main().

5. Class widget should have a constructor.

#include <iostream>
using namespace std;

class widget
{
public:
widget():info(0){}
int info;
};

void changer (int i, widget *x)
{
cout << i << "\n";
x->info = i;
cout << x->info << "\n";
}

int main()
{
widget w;
w.info = 12;

cout << w.info << "\n";
changer (10, &w);
cout << w.info << "\n"; //I want this to output 10 not 12.

system("PAUSE");
return 0;
}
Mar 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: Jim Langston | last post by:
I understand the rule of three, that if I have a custom constructor, copy or destructor I probably need the other 2. My class object definately has a custom constructor and destructor, but I'm...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
3
by: questions? | last post by:
I tried to pass a two dimensional array in the function arguments the following program is a demonstration, ******************************************** # include <stdio.h> # include...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
3
by: Jim | last post by:
Ok, I'm having 'fun' with pointers to structures. I've got some code that looks something like this: ==================== typedef struct { unsigned long *clno, *lastHistoryRecord; } aRecord;
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.