-
2 Begginer questions...
Hey, this is my first post and im learning how to program in c++
As i advance in the begginer lessons, 2 questions have been wondering inside my head:
1. How do you use pointers to change, from function A, the value of a variable from another function B? (Or, how do you do it at all :P)
2. How can u return structures from functions? IE:
Being database a predefined struct?
What are the practical uses for this?
Thanx man
-
1. You make the parameter of the function B a pointer, then pass the address of the variable to the function. In C++, it is usually easier to use references instead. In that case, you would make the parameter of B a reference, and you pass the variable from A normally. Any changes to the parameter in B will affect the variable in A.
2. Your code is correct. There are too many uses for this to list them all or to even start. In C++, a very common design goal is to make structs and classes (which are virtually the same thing) act like built-in types. That means that you pass them to functions or return them from functions just as you would a type like int or double.
-
Thanx :)
Man i get trouble when returning structures from functions...
I get the following error message:
Code:
.\Starter.cpp(22) : error C2556: 'main::data fn(void)' : overloaded function differs only by return type from 'char fn(void)'
.\Starter.cpp(6) : see declaration of 'fn'
-
-
Basically I define the struct, then create a function with return type of the struct type, but i dont know what to set in the return value of that function...
-
The error is because you have two functions both named fn, one that returns char and one that returns the struct.
The code should be the same for a struct or an int. Create a variable of the type of your struct and return it. You really need to post the exact code if you have a specific problem.