//Demontration program for the use of pointers
int intMenu = 10;
//================================================================
void setup()
{
Serial.begin(9600);
Serial.println( (int) &intMenu, DEC); // print the address of variable intMenu
} //End setup
//================================================================
void loop() {
Mastercontroller(&intMenu);
Serial.print("intMenu = ");
Serial.println(intMenu);
delay(300);
}
//================================================================
void Mastercontroller(int *intMenu) {
int temp;
temp = *intMenu;
*intMenu = ++temp;
}
First, if you want to see the memory address of where the variable is stored (its lvalue), you need to use the address of operator (&) and cast it as an int as shown in setup(). I initialized intMenu to 10 at the point where it is defined. The default way of passing data in C is "pass by value", which means a temporary variable is placed on the stack and passed to your Mastercontroller() function. If you want the function permanently change a parameter, you must "pass by reference", which means you need to tell your function where the parameter "lives in memory". Since it is the lvalue that tells where a variable is located in memory, you use the address of operator in front of the parameter when you call the function, as shown in loop(). Now the compiler sends the memory address of the variable, rather than a copy of its value. However, the compiler needs to know that it has received a memory address (lvalue) rather than the actual value of the variable (its rvalue). You do this with:
void Mastercontroller(int *intMenu) {
in the function's signature. Note you must use the type specifier (int) in the signature so the compiler knows how many bytes of data are associated with the variable being pointed to. I assigned the the value into temp so you can print it out there if you wish. I then increment its value and assign it back into the parameter. This permanently changes the parameter as you can see on the output.
Thanx Econjack for your help and explaination. It works fine.
Although it seems minor changes, they are very essential.
I think that some people don't realize that global parameters can be read everywhere in a program, but changes of a global variable only has effect within a function.
No, global variables can be changed everywhere, that's why you want to hide your data as much as possible. (Hiding your data is called encapsulation in OOP languages.) Anyway, move intMenu inside of loop(), which better illustrates how you can hide a variable inside a function but have another function permanently change its value:
//Demontration program for the use of pointers
//================================================================
void setup()
{
Serial.begin(9600);
} //End setup
//================================================================
void loop() {
static int intMenu = 10; // Not global any more.
Serial.println( (int) &intMenu, DEC); // print the address of variable intMenu
Mastercontroller(&intMenu);
Serial.print("intMenu = ");
Serial.println(intMenu);
delay(300);
}
//================================================================
void Mastercontroller(int *intMenu) {
int temp;
temp = *intMenu;
*intMenu = ++temp;
}
Note the use of the storage specifier static. If you leave that out, intMenu would get reset to 10 on each pass through loop().