//flex table opened by JP

Click to See Complete Forum and Search --> : Me and my C++ questions lol


nothing
12-28-2003, 03:42 PM
Consider this piece of code:

#include <iostream>
using std::cout;
using std::endl;

void bla(void);

int main()
{
int num1 = 10;
int num2;

cout << num1 << " " << num2 << endl;

return 0;
}

void bla(void)
{
int num3 = 20;
int num4 = 30;
int num5 = 40;
}


How do I assign the value of variable num4, which is inside function bla, to variable num2, which is inside function main? Is this where pointers come in too? :D

Dark_Raver
12-29-2003, 02:00 AM
there is a number of ways that could be done.
since you do not call the function blah(void) anywhere in your code it is hard to say which might be best.

but i can think of 3 off the top of my head.
1.
if you call blah in main, pass in a pointer to num2 and have the value of num4 assigned to it in the end of the function or wherever it is of the value you want.

2.
have blah return a variable. if you return the value of num4, you can assign the return value of the function to num2 when you call blah.

3.
have a global variable that gets assigned the value of num4 inside blah, and then assigned to num2 inside main.
but this is not the best way. globals should be avoided at all cost, and only used is special circumstances.

number one is a clean and fast one but involves more knowledge of c++. number 2 is the simplest one to implement but only allows you to return a single value unless you want to return an array. at which point you shoot performance in the foot.

so number one might be the best way to go and pointers are one of the best aspects in c++ even though they can get a bit confusing.
they allow you to manipulate and pass data between functions at optimal speed so it is definately something you want to know if you are serious about c++



DR

nothing
12-29-2003, 07:35 AM
I thought about pointer but I am not sure how to do it. Could you show me a little example please?

Dark_Raver
12-29-2003, 01:54 PM
here is an example how it might work in current code bit


void bla(int* p_number); // the function accepts input of type int pointer

int main()
{
int num1 = 10;
int num2;

blah(&num2); // when you call the function you pass in the address of num2
// you do that by using the addressof operator ( & )
// this doesn't pass the value but the actual memory address

cout << num1 << " " << num2 << endl;

return 0;
}

void bla(int* p_number) // the function accepts input of type int pointer
{
int num3 = 20;
int num4 = 30;
int num5 = 40;
*p_number = num4; // when you dereference the pointer using * the value of
// num4 is assigned to the memory location of num2

// p_number = num4; would change the memory address held in the pointer.
// *p_number = &num4; would copy the memory address of num4 into p_number
// and p_number would become an invalid pointer as soon as num4 goes out of scope
}




this is a very simple example and pointers can cause a lot of trouble and debugging hours if not used and understood properly.

i would advise you to really put some time into learning about pointers if you are any serious about c++.



DR

nothing
12-30-2003, 07:34 AM
That will definitely do it. Thank you.