Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Passing data between functions?
New Posts  All Forums:Forum Nav:

C++ Passing data between functions? - Page 4

post #31 of 36
Thread Starter 
Quote:
Originally Posted by Tomiger View Post
Hahah. Well now you have the time to make it all neat. Did you get it working with the pointers and what not? And yeah, I'd use the new code I posted up over the other one. Not only is it better programming practice, but it's much more user-friendly. (make sure you use the freshly edited code in my post; the one in your quote won't really work, as I figured out when i compiled).
Actually I turned it in still. =/ But im gonna keep a note of this thread and I will always have it.
post #32 of 36
In your second code sample, you were using pointers, but not actually passing a pointer. A pointer holds the address of a variable. You were passing a double to a function that takes a double*, so it was interpreting the value of the double as a memory address.

How to pass a reference:
int i = 5;
blah(i);

void blah(int & number) {
printf("%d", number);
}

How to pass a pointer (there are few reasons to use these in C++):
int i = 5;
int * i_p = &i;
blah(i_p);

void blah(int * number) {
printf("%d", *number);
}


You should not be using pointers or references for this exercise. Pass everything by value. As a rule of thumb, don't use anything you weren't taught about yet. I assume you weren't taught (much) about references or pointers.
Edited by Coma - 1/28/11 at 8:23am
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #33 of 36
Thread Starter 
Quote:
Originally Posted by Coma View Post
In your second code sample, you were using pointers, but not actually passing a pointer. A pointer holds the address of a variable. You were passing a double to a function that takes a double*, so it was interpreting the value of the double as a memory address.

How to pass a reference:
int i = 5;
blah(i);

void blah(int & number) {
printf("%d", number);
}

How to pass a pointer (there are few reasons to use these in C++):
int i = 5;
int * i_p = &i;
blah(i_p);

void blah(int * number) {
printf("%d", *number);
}


You should not be using pointers or references for this exercise. Pass everything by value. As a rule of thumb, don't use anything you weren't taught about yet. I assume you weren't taught (much) about references or pointers.
Talked about in class but not demonstrated yet. I was just trying to learn something new. Oh well. As you can see in the last code I put in there I just passed everything as arguments in the functions. Thanks for your input.
post #34 of 36
Don't do that with C++. There are so many intricacies and little things that don't work as you would expect that trying to do something on your own is almost guaranteed to eventually lead to disaster.

You can buy the C++ book by Stroustrup and use it as a reference - it mentions a lot of those.
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #35 of 36
Thread Starter 
Quote:
Originally Posted by Coma View Post
Don't do that with C++. There are so many intricacies and little things that don't work as you would expect that trying to do something on your own is almost guaranteed to eventually lead to disaster.

You can buy the C++ book by Stroustrup and use it as a reference - it mentions a lot of those.
We are going over pointers next week in class.
post #36 of 36
Quote:
Originally Posted by Timlander View Post
We are going over pointers next week in class.
Have fun with linked lists
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Passing data between functions?