Last active
April 20, 2016 21:30
-
-
Save futureshocked/c548c64abb5caced4bdc4981ec02df1b to your computer and use it in GitHub Desktop.
A sketch written to help people understand the use of references and pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ref_vs_pointers | |
* A sketch written to help people understand the use of references and pointers | |
* Written by Peter Dalmaris, April 8 2016. | |
* Arduino Tips and Tricks | |
* txplore.com | |
* | |
* Requirements: MemoryFree from https://github.com/McNeight/MemoryFree | |
*/ | |
#include <MemoryFree.h> | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
print_free_memory(); | |
int a = 5; | |
int b = 10; | |
int *pointer; // Create a new pointer. Variable "pointer" will contain a memory address where | |
// an int value will be stored | |
print_free_memory(); | |
// Play with pointer to variable a | |
Serial.println(F("1 ----- a -----")); | |
Serial.print(F("a=")); | |
Serial.println(a); | |
pointer = &a; //Get the address for variable "a" and assign it to pointer "pointer" | |
*pointer = 6; // Store value "6" to memory location stored in "pointer". | |
Serial.print(F("a=")); | |
Serial.println(a); | |
print_free_memory(); | |
Serial.println(); | |
// Play with pointer to variable b | |
Serial.println(F("2 ----- b -----")); | |
Serial.print(F("b=")); | |
Serial.println(b); | |
pointer = &b; //Get the address for variable "b" and assign it to pointer "pointer" | |
// This will reassign the memory location stored in "pointer" from the original | |
// memory location for int "a", to that of int "b". | |
*pointer = 11; // Store value "11" to memory location stored in "pointer". | |
Serial.print(F("b=")); | |
Serial.println(b); | |
print_free_memory(); | |
Serial.println(); | |
// Play with passing data to a function via reference | |
pass_reference(a); //Even though we are passign int "a", the function expects a reference as a parameter. | |
//Notice that the function declaration uses the "&" operator. This will retrieve | |
//the memory location for int "a" instead of making a copy for it. | |
pass_reference(b); //Exactly as above. | |
// Play with passing data to a function via pointer | |
pass_pointer(pointer); | |
pointer = &a; | |
pass_pointer(pointer); | |
//Play with passing data to a function via value | |
pass_value(a); | |
pass_value(b); | |
//Do all of the above using a String object (instead of primitives) | |
String a_string = "Hello World!"; | |
String b_string = "Hello again!"; | |
String *string_pointer; | |
//Print the content of the pointer; | |
string_pointer = &a_string; | |
Serial.println(F("6 ------- String a -----")); | |
Serial.print(F("string a=")); | |
Serial.println(*string_pointer); | |
print_free_memory(); | |
Serial.println(); | |
string_pointer = &b_string; | |
Serial.println(F("7 ------- String b -----")); | |
Serial.print(F("string b=")); | |
Serial.println(*string_pointer); | |
print_free_memory(); | |
Serial.println(); | |
// Play with passing string to a function via reference | |
pass_reference_str(a_string); | |
pass_reference_str(b_string); | |
// Play with passing string to a function via pointer | |
// pointer = &a_string; | |
pass_pointer_str(&a_string); | |
// pointer = &a_string; | |
pass_pointer_str(&b_string); | |
//Play with passing data to a function via value | |
pass_value_str(a_string); | |
pass_value_str(b_string); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} | |
void pass_reference(int &a_ref) | |
{ | |
Serial.println(F("3 ----- Passing by reference -----")); | |
Serial.println(a_ref); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void pass_pointer(int *a_pointer) | |
{ | |
Serial.println(F("4 ----- Passing by pointer -----")); | |
Serial.println(*a_pointer); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void pass_value(int value) | |
{ | |
Serial.println(F("5 ----- Passing by value-----")); | |
Serial.println(value); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void pass_reference_str(String &a_ref) | |
{ | |
Serial.println(F("8 ----- Passing String by reference -----")); | |
Serial.println(a_ref); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void pass_pointer_str(String *a_pointer) | |
{ | |
Serial.println(F("9 ----- Passing String by pointer -----")); | |
Serial.println(*a_pointer); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void pass_value_str(String value) | |
{ | |
Serial.println(F("10 ----- Passing String by value-----")); | |
Serial.println(value); | |
print_free_memory(); | |
Serial.println(); | |
} | |
void print_free_memory(){ | |
Serial.print(F("freeMemory()=")); | |
Serial.println(freeMemory()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment