Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 20th, 2005, 12:01 AM   #1
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
Pointers to return multiple values

I'm steadily progressing through C++ and am looking at the moment at Pointers vs. References. I have a little question and turned here for it to be answered ...

 

#include <iostream>

using namespace std;

short Factor(int n, int* pSquared, int* pCubed);

int main()
{
	int number, squared, cubed;
	short error;

	cout << "Enter a number (0 - 20): ";
	cin >> number;

	error = Factor(number, &squared, &cubed);

	if (error)
	{
		cout << "Number: " << number << endl;
		cout << "Square: " << squared << endl;
		cout << "Cubed " << cubed << endl;
	}
	else
		cout << "Error encountered!!" << endl;
	return 0;
}

short Factor(int n, int *pSquared, int *pCubed)
{
	short Value = 1;
	if (n > 20)
		Value = 0;
	else
	{
		*pSquared = n*n;
		*pCubed = n*n*n;
		
	}
	return Value;
}

My problem arizes because I'm used to seeing things (such as pointers) clearly initialized, yet here ... I can't seem to see line that initializes the pointers that are used.

I can see they're mentioned in the prototype of Factor, but that doesn't initialize them as usable pointers that can be legitimately used from there on ... does it? Even so, I can't see a place where the pointers are tied to gether with the 'squared' and 'cubed' variable integers anyway.

I'd be pleased with a short, clear and concise explanation if possible.
Clotters is offline   Reply With Quote
Old Jul 20th, 2005, 12:12 AM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
 
short Factor(int n, int* pSquared, int* pCubed);
int main()
{
...
	error = Factor(number, &squared, &cubed);
...
}

short Factor(int n, int *pSquared, int *pCubed)
{
	short Value = 1;
	if (n > 20)
		Value = 0;
	else
	{
		*pSquared = n*n;
		*pCubed = n*n*n;
		
	}
	return Value;
}
Quote:
My problem arizes because I'm used to seeing things (such as pointers) clearly initialized, yet here ... I can't seem to see line that initializes the pointers that are used.
the addresses of variables are passed, the variables already are declared and exist, they are integers.

Quote:
but that doesn't initialize them as usable pointers that can be legitimately used from there on ... does it?
what's happening is that whatever that address contains is being manipulated, which is why * is used, i.e. the contents of the address.

Quote:
Even so, I can't see a place where the pointers are tied to gether with the 'squared' and 'cubed' variable integers anyway.
in red.

ps: check out this thread
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 20th, 2005, 7:13 PM   #3
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
Thanks, for that. A new issue has arrived though.

You might want to bear in mind that this is the first time I'm being ontroduced to copy constructors.

// Passing Objects by Reference
#include &lt;iostream&gt;

using namespace std;
class SimpleCat
{
  public:
     SimpleCat ();           // constructor
     SimpleCat (SimpleCat&); //copy consructor (why have SimpleCat& inside the brackets? Does the & do anything)
     ~SimpleCat();           // destructor
};

SimpleCat::SimpleCat()
{   
   cout &lt;&lt; "Simple Cat Constructor..." &lt;&lt; endl;
}

SimpleCat::SimpleCat(SimpleCat&)
{
   cout &lt;&lt; "Simple Cat Copy Constructor.." &lt;&lt; endl;
}

SimpleCat::~SimpleCat()
{
  cout &lt;&lt; "Simple Cat Destructor.." &lt;&lt; endl;
}

SimpleCat FunctionOne (SimpleCat theCat);   //
SimpleCat* FunctionTwo (SimpleCat *theCat); // 

int main()
{
   cout &lt;&lt; "Making a cat..." &lt;&lt; endl;
   SimpleCat Frisky;
   cout &lt;&lt; "Calling FunctionOne..." &lt;&lt; endl;
   FunctionOne(Frisky);
   cout &lt;&lt; "Calling FunctionTwo..." &lt;&lt; endl;
   FunctionTwo(&Frisky);
   char response;
   cin &gt;&gt; response;
   return 0;
}

// FunctionOne passes by value
SimpleCat FunctionOne(SimpleCat theCat)
{
   cout &lt;&lt; "Function One. Returning..." &lt;&lt; endl;
   return theCat;
}

// FunctionTwo passes by reference
SimpleCat* FunctionTwo(SimpleCat *theCat)
{
   cout &lt;&lt; "Function Two. Returning..." &lt;&lt; endl;
   return theCat;
}

Now the output SHOULD, in theory, be ...

Making a cat ...
Simple Cat Constructor...
Calling Function One...
Simple Cat Copy Constructor..
FunctionOne. Returning...
Simple Cat Copy Constructor...
Simple Cat Destructor...
Calling Function Two...
Function Two. Returning...
Simple Cat Destructor...

Now, would someone please be kind enough tell me what is going on and the basic message of this example that I need to understand.

Thanks in advance.
Clotters is offline   Reply With Quote
Old Jul 20th, 2005, 7:43 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A pointer is a reference in the dictionary sense, as is a nickname. In C++, the term "reference" was chosen (perhaps unwisely) to indicate an alias, and not just any old reference. A pointer is a separate variable which contains the address of a thing. I have a tutorial regarding pointers at the link in my signature. It does not cover references. A reference (C++ sense) is an alias, merely another name for an existing item. When you deal with a pointer, you deal with an item indirectly. When you deal with a reference, you deal with an item directly, but you call it "redneck" instead of "Jim Bob." When you copy an object you might get by with a "shallow" copy, which means it might contain pointers to other objects and you just copy those pointers. If the referred to object is deleted, all pointers to it become invalid. A non-shallow copy might make a copy of the objects and assign them new pointers. That way, if the original were destroyed, there would still be a valid object for the copy.

The use of the "&" has to be derived from context (in C++). It might be used as the "address of" operator and it might be used to assign an alias to an existing object. When you pass a reference to a function, the parameter is actually a pointer to the object, but when you pass a pointer you are actually creating a copy of a pointer. The effective references differ by one level of indirection. The compiler takes this into consideration so that you use one directly (syntactically speaking) inside the function and the other indirectly.

I'm not sure if this addresses your question. Post back if not. A pointer is a reference, but not a "C++ reference." A convict and his number are not the same, but "Bent Nose" Billy the Kid and William Bonney might very well be.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 20th, 2005, 7:50 PM   #5
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
Thanks, that helps. One other thing I was worried about was that I know references can not be null. Some compilers might still compile it, but if a nulled reference is used, it will cause a problem.

My question is how do you create a null reference, so I know what they are.

Simple assigning a reference to a variable containing the null number (0) doesn't crash the system, like I was hoping.

This may seem odd, but my logic is if I know how to make a null referece, I'll know what they are .. and be able to stop them from occuring.
Clotters is offline   Reply With Quote
Old Jul 20th, 2005, 7:52 PM   #6
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Example you posted definitely does not demonstrate purpose and usefullness of a copy constructor.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 20th, 2005, 7:53 PM   #7
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <iostream>

int main(int argc, char *argv[]) {
	int *pntr = NULL;
	*pntr = 82;
	std::cout << pntr << std::endl;
	return 0;
}

I think the code above will throw an error.
__________________

tempest is offline   Reply With Quote
Old Jul 20th, 2005, 8:02 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Tempest's example doesn't even contain a reference. A reference (C++) can't be NULL because you make a reference by saying "ThisName = &ThatName". If you assign zero to ThisName you just change the value of ThisName AND ThatName (the same bloke, you see), you don't create a null reference.

A null pointer is another horse. Whether or not a null pointer causes a problem depends upon the circumstance. See the last section of my tutorial for material on NULL pointers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 20th, 2005, 9:54 PM   #9
Clotters
Programmer
 
Join Date: May 2005
Posts: 60
Rep Power: 4 Clotters is on a distinguished road
Thanks for that help.

I think I understand the fundamentals of pointers and references. At the very least I can interpet most pieces of code using them now.

I've been on them for a while, what I think I need to do is take a break. I'll finish the rest of my book, and then go over in detail the pointer and reference chapters again.

After a small break I think I'll be able to go back and tackle the tougher issues better than I am able to now, anyway.

Once again, thanks for the help.
Clotters is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC