{"id":17151,"date":"2023-01-13T20:12:44","date_gmt":"2023-01-13T20:12:44","guid":{"rendered":"https:\/\/wolles-elektronikkiste.de\/?p=17151"},"modified":"2025-01-25T15:18:26","modified_gmt":"2025-01-25T15:18:26","slug":"pointers-and-references","status":"publish","type":"post","link":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references","title":{"rendered":"Pointers and references"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">About this post<\/h2>\n\n<p>Pointer and references &#8211; what a dry topic! So, why did I write a whole post on this subject? In my experience, not only absolute beginners occasionally have problems with it, especially with the use pointers. Therefore, I have tried to present the topic systematically, understandably and with many example sketches. This post is probably still challenging readers with less experience. But from my point of view, learning how to use pointers is worth the effort because you can&#8217;t avoid the subject and because it is essential for a real understanding of C++.<\/p>\r\n<p>In addition, this article is intended to form the basis for the follow-up article, which will deal with the SRAM.<\/p>\r\n<p>This is the content:<\/p>\r\n<ul>\r\n<li><a href=\"#pointer\">Pointers<\/a>\r\n<ul>\r\n<li><a href=\"#what_is_a_pointer\">What is a pointer?<\/a><\/li>\r\n<li><a href=\"#simple_example\">A simple example<\/a><\/li>\r\n<li><a href=\"#deepening\">Going deeper<\/a><\/li>\r\n<li><a href=\"#reading_memory_using_pointers\">Reading an area of memory using pointers<\/a><\/li>\r\n<li><a href=\"#arrays\">Arrays<\/a><\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><a href=\"#references\">References<\/a><\/li>\r\n<li><a href=\"#passing_parameters\">Parameter passing<\/a>\r\n<ul>\r\n<li><a href=\"#call_by_value\">Call-by-Value<\/a><\/li>\r\n<li><a href=\"#call_by_reference\">Call-by-Reference<\/a>\r\n<ul>\r\n<li><a href=\"#too_many_return_values\">Too many return values needed?<\/a><\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><a href=\"#call_by_pointer\">&#8220;Call-by-Pointer&#8221;<\/a>\r\n<ul>\r\n<li><a href=\"#passing_objects\">Passing objects<\/a><\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><a href=\"#passing_arrays\">Pass arrays<\/a><\/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\n<h2 class=\"wp-block-heading\" id=\"pointer\">Pointers<\/h2>\n\n<h3 class=\"wp-block-heading\" id=\"what_is_a_pointer\">What is a pointer?<\/h3>\n\n<p>Before we get to the question of what a pointer is, let&#8217;s take a step back: What is a variable? A variable is a reserved place for data that you can change during the execution of the program. A variable has a name, a data type, a specific address and a specific value. The data type determines the memory space to be reserved.<\/p>\r\n\n<p>A pointer is also a variable. However, it is special in that its value is the (initial) memory address of another variable or object. So, the pointer does what its name suggests: It points to something else.&nbsp;<\/p>\r\n<p>When defining the pointer, the data type of the &#8220;target&#8221; must be specified. Without this additional information, it would not be known at which address the target ends in memory and how to interpret it.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"simple_example\">A simple example<\/h3>\n\n<p>Since these explanations are very theoretical, let&#8217;s have a look at a practical example.<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"pointer_basic.ino\" data-enlighter-title=\"pointer_basic.ino\">void setup(){\r\n  Serial.begin(9600);\r\n  \r\n  int iVal = 42;\r\n  Serial.print(\"iVal = \");\r\n  Serial.println(iVal);\r\n  \r\n  int *iPointer;  \/\/ alternative: int* iPointer;\r\n  iPointer = &amp;iVal;\r\n  Serial.print(\"*iPointer = \");\r\n  Serial.println(*iPointer);\r\n  \r\n  iVal += 4200;\r\n  Serial.print(\"iVal = \");\r\n  Serial.println(iVal);\r\n  Serial.print(\"*iPointer = \");\r\n  Serial.println(*iPointer);\r\n}\r\n\r\nvoid loop(){}<\/pre>\r\n\n<p>And this is the output:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Pointer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Pointer.png\" alt=\"Pointer in action: output pointer_basic.ino\" class=\"wp-image-16294\" width=\"634\" height=\"162\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Pointer.png 634w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Pointer-300x77.png 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/a><figcaption class=\"wp-element-caption\">Output pointer_basic.ino<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\">Explanations to the sketch<\/h4>\n\n<p>First, the integer variable iVal is declared and assigned the value 42.<\/p>\r\n<p>With <code>int *iPointer<\/code> we create the pointer iPointer. The character <code>*<\/code> specifies that this is a pointer. <code>int<\/code> specifies that the target, i.e. the object to which the pointer points, is an integer.<\/p>\r\n<p>However, at the time of declaration, we have not yet defined where the target is, i.e. where the pointer points to. We achieve this with <code>iPointer = &amp;iVal;<\/code>. iPointer then points to the address of iVal. The character <code>&amp;<\/code> is the address operator.<\/p>\r\n<p>iPointer is the pointer itself. To get the value pointed to by iPointer, you must prefix the pointer with the indirection operator <code>*<\/code>. The indirection operator is also called the dereference operator.<\/p>\r\n<p>If we now change the variable iVal, it is still at the same address. Therefore, the instruction <code>iVal += 4200;<\/code> has no effect on iPointer, but it does on its target value *iPointer.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"deepening\">Going deeper<\/h3>\n\n<p>I wrote the following sketch to expand on the topic:<\/p>\r\n\n<div class=\"scroll-paragraph\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"basic_pointer_2.ino\" data-enlighter-title=\"basic_pointer_2.ino\">void setup(){\r\n  Serial.begin(9600);\r\n  delay(2000); \/\/ needed for some boards\r\n\r\n  int iVal = 4242;\r\n  int *iPointer;\r\n  iPointer = &amp;iVal;\r\n   \r\n  long iLongVal = 42424242;\r\n  long *iLongPointer;\r\n  iLongPointer = &amp;iLongVal;\r\n\r\n  long long iLongLongVal = 4242424242424242;\r\n  long long *iLongLongPointer;\r\n  iLongLongPointer = &amp;iLongLongVal;\r\n  \r\n  Serial.print(\"iVal             = \");\r\n  Serial.println(iVal);\r\n  Serial.print(\"iLongVal         = \");\r\n  Serial.println(iLongVal);\r\n  Serial.println(\"Sorry, can't print iLongLongVal\"); \r\n   *iLongPointer += 1;\r\n  Serial.print(\"Updated iLongVal = \");\r\n  Serial.println(iLongVal);\r\n  Serial.println();\r\n\r\n  Serial.print(\"iVal address     = \");\r\n  Serial.println((int)&amp;iVal);\r\n  Serial.print(\"iPointer value   = \");\r\n  Serial.println((unsigned int)iPointer);\r\n  Serial.print(\"iPointer address = \");\r\n  Serial.println((int)&amp;iPointer);\r\n  Serial.print(\"*iPointer        = \");\r\n  Serial.println(*iPointer);\r\n  Serial.println();\r\n   \r\n  Serial.print(\"Size of iVal         = \");\r\n  Serial.println(sizeof(iVal));\r\n  Serial.print(\"Size of iLongVal     = \");\r\n  Serial.println(sizeof(iLongVal));\r\n  Serial.print(\"Size of iLongLongVal = \");\r\n  Serial.println(sizeof(iLongLongVal));\r\n  Serial.println(); \r\n  \r\n  Serial.print(\"Size of iVal address         = \");\r\n  Serial.println(sizeof(&amp;iVal));\r\n  Serial.print(\"Size of iLongVal address     = \");\r\n  Serial.println(sizeof(&amp;iLongVal));  \r\n  Serial.print(\"Size of iLongLongVal address = \");\r\n  Serial.println(sizeof(&amp;iLongLongVal));  \r\n  Serial.println(); \r\n  \r\n  Serial.print(\"Size of iPointer         = \");\r\n  Serial.println(sizeof(iPointer));\r\n  Serial.print(\"Size of iLongPointer     = \");\r\n  Serial.println(sizeof(iLongPointer));\r\n  Serial.print(\"Size of iLongLongPointer = \");\r\n  Serial.println(sizeof(iLongLongPointer));\r\n}\r\n\r\nvoid loop(){\/* empty *\/}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<p>A <strong>hint<\/strong> that has nothing to do with pointers: With so many <code>Serial.print(\"text\")<\/code> instructions, consider switching to <code>Serial.print(F(\"text\"))<\/code> to save RAM at the expense of flash (more on this <a href=\"https:\/\/www.arduino.cc\/reference\/de\/language\/functions\/communication\/serial\/print\/\" target=\"_blank\" rel=\"noopener\">here<\/a>). I have consistently refrained from doing so to focus on the essentials.&nbsp;<\/p>\r\n\n<p>Back to the main topic: before we get to the explanations, here is the output I got when using an Arduino Nano:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_2_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"541\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_2_output.png\" alt=\"Pointer in action: output basic_pointer_2.ino\" class=\"wp-image-16315\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_2_output.png 850w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_2_output-300x191.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_2_output-768x489.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/a><figcaption class=\"wp-element-caption\">Output basic_pointer_2.ino<\/figcaption><\/figure>\n<\/div>\n<h4 class=\"wp-block-heading\">Explanations<\/h4>\n\n<p>In the sketch, three variables of type Integer, Long Integer and Long Long Integer are defined and output. Long Integer cannot be output with <code>Serial.print()<\/code>, but that is another topic. In addition to the three variables, pointers are defined that point to the variables. <\/p>\r\n<p>In line 21, *iLongPointer is incremented. As you can see, this changes the value of iLongVal. This is basically unsurprising, since iLongPointer points to iLongVal.<\/p>\r\n<p>Using iVal as an example, let&#8217;s look in detail at what makes the difference between the variable itself and a pointer to it. Then value of iVal is 4242 and iVal located at memory address 2298. 2298 is the starting address. The size of iVal depends on the data type and the microcontroller used. To get the address of iVal, we use the address operator <code>&amp;<\/code>. To output the address, it must still be explicitly converted to an integer, e.g. like this: <code>(unsigned long)&amp;iVal<\/code>.<\/p>\r\n<p>iPointer has its own address, namely 2296. The access to the address works the same way as with iVal. The value of the pointer, i.e. what you read at its own memory address, is the target address (i.e. the address of iVal), namely 2298.<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_vs_integer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_vs_integer.png\" alt=\"Variable vs. pointer to the variable\" class=\"wp-image-16308\" width=\"475\" height=\"123\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_vs_integer.png 950w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_vs_integer-300x77.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_vs_integer-768x198.png 768w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/a><figcaption class=\"wp-element-caption\">Variable vs. pointer to the variable<\/figcaption><\/figure>\n<\/div>\n<p>In the following lines of the sketch determines the space needed for the different integer types (int, long, long long). On an ATmega328P based Arduino these are 2, 4 and 8 bytes. &#8220;Homework for you&#8221;: run the sketch on an ESP32, ESP8266 or SAMD board and see the difference.<\/p>\r\n<p>The addresses of the integer values have a size of 2 bytes on an ATmega328P based board. This corresponds to the space required by the pointers. So it doesn&#8217;t matter how big the object the pointer points to is &#8211; the memory needed for the pointer is always the same.&nbsp;<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"reading_memory_using_pointers\">Reading an area of memory using pointers <\/h3>\n\n<p>As you have seen, you can output the value of a variable by dereferencing the pointer to the variable with the <code>*<\/code> operator. But would it be possible to read the content of the memory at any address? The answer is: Yes and No! Reading the memory itself is no problem &#8211; but up to which address? And is it a character or an integer or a float?&nbsp;<\/p>\r\n<p>You simply have to know the type of the data to be read. The reading is done indirectly using a pointer. Here are the instructions as pseudocode:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">datatype *anyPointer;\r\nanyPointer = (datatype*)address;\r\nValue of anyPointer = *anyPointer;\r\n\/\/ In short \/ kurz:\r\ndatatype anyPointer = *(datatype*)anyPointer;\r\n<\/pre>\r\n\n<p>In words: 1) Define a pointer and apply the correct variable type, 2) Assign the address to be read to the pointer, 3) Access the value by dereferencing the pointer.<\/p>\r\n<p>The following sketch plays with this a bit by interpreting the content of a memory area in different ways. <\/p>\r\n\n<div class=\"scroll-paragraph\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"basic_pointer_3.ino\" data-enlighter-title=\"basic_pointer_3.ino\">void setup(){\r\n  Serial.begin(9600);\r\n  delay(2000);\r\n   \r\n  long iLongVal = 42424242;\r\n  long *iLongPointer;\r\n  iLongPointer = &amp;iLongVal;\r\n  \r\n  Serial.print(\"iLongVal = \");\r\n  Serial.println(iLongVal);\r\n \r\n  Serial.print(\"iLongVal address = \");\r\n  Serial.println((int)&amp;iLongVal);\r\n\r\n  Serial.print(\"*iLongPointer = \");\r\n  Serial.print(*iLongPointer);\r\n  Serial.print(\" = 0x\");\r\n  Serial.println(*iLongPointer, HEX);\r\n  Serial.println();\r\n\r\n  byte *jVal;  \/\/ alternative to byte: unsigned char\r\n  byte *kVal;\r\n  byte *lVal;\r\n  byte *mVal;\r\n  int *nVal;\r\n  int *oVal;\r\n  long *pVal;\r\n  char *kChar;\r\n  \r\n  jVal = (byte*)&amp;iLongVal;\r\n  kVal = (byte*)((int)&amp;iLongVal + 1); \r\n  lVal = (byte*)((int)&amp;iLongVal + 2); \r\n  mVal = (byte*)((int)&amp;iLongVal + 3); \r\n  nVal = (int*)((int)&amp;iLongVal);\r\n  oVal = (int*)((int)&amp;iLongVal + 2);\r\n  pVal = (long*)((int)&amp;iLongVal);\r\n  kChar = (char*)((int)&amp;iLongVal + 1); \r\n  \r\n  Serial.print(\"*jVal @ address of iLongVal   = 0x\");\r\n  Serial.println(*jVal, HEX);\r\n  Serial.print(\"*kVal @ address of iLongVal+1 = 0x\");\r\n  Serial.println(*kVal, HEX);\r\n  Serial.print(\"*lVal @ address of iLongVal+2 = 0x\");\r\n  Serial.println(*lVal, HEX);\r\n  Serial.print(\"*mVal @ address of iLongVal+3 = 0x\");\r\n  Serial.println(*mVal, HEX); \r\n  Serial.print(\"*nVal @ address of iLongVal   = 0x\");\r\n  Serial.println(*nVal, HEX);\r\n  Serial.print(\"*oVal @ address of iLongVal+2 = 0x\");\r\n  Serial.println(*oVal, HEX);\r\n  Serial.print(\"*pVal @ address of iLongVal   = 0x\");\r\n  Serial.println(*pVal, HEX);\r\n  Serial.print(\"*kChar @ address of iLongVal+1 = \");\r\n  Serial.println(*kChar);  \r\n}\r\n\r\nvoid loop(){\/* empty *\/}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<h4 class=\"wp-block-heading\">Explanations<\/h4>\n\n<p>First, we define the variable iLongVal so that we have something to read. The variable extends over four bytes (on an ATmega328P board). In the next step we read all four bytes individually, then two each as integer and again all four as long integer. Finally, we read a single byte and interpret it as a character. As you will see, the value of this byte is 0x57 and that is a &#8220;W&#8221; according to the ASCII table.<\/p>\r\n\n<p>The structure of the content of the memory area under consideration becomes clearest when using hexadecimal numbers. Here is the output:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_3__output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"397\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_3__output.png\" alt=\"Output basic_pointer_3.ino\" class=\"wp-image-16359\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_3__output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_3__output-300x138.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pointer_3__output-768x353.png 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output basic_pointer_3.ino<\/figcaption><\/figure>\n<\/div>\n<p>The practical use of this approach is very limited because we should know what we have written where in the memory. Sorry that I share this only now \ud83d\ude09 . However, I thought that this would be quite useful to further deepen your knowledge about pointers.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"arrays\">Arrays<\/h3>\n\n<p>Strictly speaking, in the field of microcontrollers \/ Arduino one means C arrays when talking about arrays. In C++ there is the much more comfortable class vector. That C arrays are used anyway is simply a matter of limited resources. C arrays are faster and require less memory.<\/p>\r\n<p>When you define a (C) array, the name of the array is also the identifier of the pointer to element 0 of the array. This is easier than it may sound. It just means that instead of <code>anyArray[0]<\/code> you might as well write <code>*anyArray<\/code>.<\/p>\r\n\n<p>The following sketch plays a bit with the pointer properties of arrays:<\/p>\r\n\n<div class=\"scroll-paragraph\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"array_play.ino\" data-enlighter-title=\"array_play.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  delay(2000); \/\/ needed for some boards\r\n  \r\n  int intArray[4] = {4, 42, 424, 4242};\r\n  for(int i=0; i&lt;4; i++){\r\n    Serial.print(\"intArray[\");\r\n    Serial.print(i);\r\n    Serial.print(\"] = \");\r\n    Serial.println(intArray[i]);\r\n  }\r\n  Serial.println();\r\n  Serial.print(\"*intArray       = \");\r\n  Serial.println(*intArray);\r\n  Serial.print(\"*(intArray + 2) = \");\r\n  Serial.println(*(intArray + 2));\r\n  int *arrayElement_3;\r\n  arrayElement_3 = &amp;intArray[3];\r\n  Serial.print(\"*arrayElement_3 = \");\r\n  Serial.println(*arrayElement_3);\r\n  Serial.println();\r\n\r\n  int *sameArray = intArray;\r\n  Serial.println(\"sameArray[]:\");\r\n  for(int i=0; i&lt;4; i++){\r\n    Serial.print(sameArray[i]);\r\n    Serial.print(\" \");\r\n  }\r\n  Serial.println();\r\n  Serial.println(\"Listing by pointer incrementation:\");\r\n  for(int *p=intArray; p &lt; intArray + 4; p++){\r\n    Serial.print(*p);\r\n    Serial.print(\" \");\r\n  }\r\n  \r\n  Serial.println(\"\\n\");\r\n  Serial.print(\"Size of intArray  = \");\r\n  Serial.println(sizeof(intArray));\r\n  Serial.print(\"Size of sameArray = \");\r\n  Serial.println(sizeof(sameArray));\r\n  Serial.print(\"Address of intArray  = \");\r\n  Serial.println((int)&amp;intArray);\r\n  Serial.print(\"Address of sameArray = \");\r\n  Serial.println((int)&amp;sameArray);\r\n  Serial.print(\"Value of intArray  = \");\r\n  Serial.println((int)intArray);\r\n  Serial.print(\"Value of sameArray = \");\r\n  Serial.println((int)sameArray);\r\n}\r\n\r\nvoid loop() {}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<p>Here is the output first:<\/p>\r\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/12\/array__play__ouput.png\"><img loading=\"lazy\" decoding=\"async\" width=\"848\" height=\"517\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/12\/array__play__ouput.png\" alt=\"Output of array_play.ino\" class=\"wp-image-16871\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/12\/array__play__ouput.png 848w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/12\/array__play__ouput-300x183.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/12\/array__play__ouput-768x468.png 768w\" sizes=\"auto, (max-width: 848px) 100vw, 848px\" \/><\/a><figcaption class=\"wp-element-caption\">Output of array_play.ino<\/figcaption><\/figure>\n\n<h4 class=\"wp-block-heading\">Explanations<\/h4>\n\n<p>With the knowledge, you have about pointers by now, the output of the sketch is probably not a complete surprise. Nevertheless, at least two aspects are noteworthy:<\/p>\r\n<ul>\r\n<li><code>*(intArray + 2)<\/code> returns the value of element 2 of the array, even though it is &#8220;4 bytes away&#8221; from the start address on the microcontroller I used (because 2 integers = 4 bytes). On an ESP32 it would work the same way, although an integer has a size of 8 bytes there. <code>intArray + 2<\/code> therefore means &#8220;second element of intArray&#8221; and not &#8220;address of intArray + 2 bytes&#8221; or similar.\r\n<ul>\r\n<li>Incrementing in the for-construction starting at line 29 works accordingly.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>Using the pointer sameArray you can access the elements of intArray. You will not notice any difference. Nevertheless: sameArray <em>is not<\/em> intArray, even if both point to the same address. E.g. sameArray has also only a size of 2 bytes and not 8 bytes like intArray. We will come back to this when passing arrays to functions.<\/li>\r\n<\/ul>\r\n\n<h2 class=\"wp-block-heading\" id=\"references\">References<\/h2>\n\n<p>References are considerably easier to understand than pointers. A reference is simply a different identifier for the same object. An alias, so to speak. The only confusing thing is that the <code>&amp;<\/code> character is used again when defining references. With <code>int &amp;x = y<\/code> you create the alias x for the integer variable y. This is not to be confused with the already known <code>int x = &amp;y<\/code>, which assigns the variable x the value &#8220;address of y&#8221;. The character <code>&amp;<\/code> is also called reference operator, which is more appropriate than address operator in this context. <\/p>\r\n<p>Here is a simple example:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"reference_basic.ino\" data-enlighter-title=\"reference_basic.ino\">void setup(){\r\n  Serial.begin(9600);\r\n   \r\n  int iVal = 42;\r\n  int &amp;iRef = iVal; \/\/alternative: int&amp; iRef = iVal;\r\n    \r\n  Serial.print(\"iVal = \");\r\n  Serial.println(iVal);\r\n  Serial.print(\"iRef = \");\r\n  Serial.println(iRef);\r\n\r\n  iRef++;\r\n  Serial.print(\"Updated iVal = \");\r\n  Serial.println(iVal);\r\n}\r\n\r\nvoid loop(){\/* empty *\/}<\/pre>\r\n\n<p>After we have created an alias for iVal called iRef with <code>int &amp;iRef = iVal;<\/code>, both identifiers can be used for the same variable. All changes to iRef affect iVal equally and vice versa because iRef <em>is<\/em> iVal.<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"146\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output.png\" alt=\"\" class=\"wp-image-16324\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output-300x51.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output-768x130.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/reference_output-860x146.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output reference_basic.ino<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"passing_parameters\">Parameter passing to functions<\/h2>\n\n<p>Pointers and references play a major role in parameter passing to and from functions or objects. This is where their advantages really become apparent.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"call_by_value\">Call-by-Value<\/h3>\n\n<p>&#8220;Call-by-value&#8221; refers to the &#8220;normal&#8221; parameter passing to functions or objects, i.e. the way of passing that is usually learned first. Here is an example:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"call_by_value.ino\" data-enlighter-title=\"call_by_value.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  int iVar = 42;\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar);\r\n  passAsInteger(iVar);\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar);\r\n}\r\n\r\nvoid loop() {\/* empty *\/}\r\n\r\nvoid passAsInteger(int var){\r\n  var *= 2;\r\n  Serial.print(\"Variable in passAsInteger(): \"); Serial.println(var);  \r\n}<\/pre>\r\n\n<p>The variable iVar is passed to the function <code>passAsInteger()<\/code> and doubled there. The sketch outputs the value before passing, within the function and after return to setup. The output should not come as a surprise:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"140\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output.png\" alt=\"Output call_by_value.ino\" class=\"wp-image-16334\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output-300x49.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output-768x124.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_value_output-860x140.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output call_by_value.ino<\/figcaption><\/figure>\n<\/div>\n<p>When passed to the function, a copy of iVal is created in memory. The copy is deleted after return from the function, and the original iVar remains unchanged.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"call_by_reference\">Call-by-Reference<\/h3>\n\n<p>The situation is different if we pass the parameter by reference. All you have to do is prefix the parameter with the referencing operator <code>&amp;<\/code> in the receiving function:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"call_by_reference.ino\" data-enlighter-title=\"call_by_reference.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  int iVar = 42;\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar);\r\n  passAsReference(iVar);\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar); \r\n}\r\n\r\nvoid loop() {\/*&nbsp;empty&nbsp;*\/}\r\n\r\nvoid passAsReference(int &amp;var){\r\n  var *= 2;\r\n  Serial.print(\"Variable in passAsReference(): \"); Serial.println(var);  \r\n}<\/pre>\r\n\n<p>The output is:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"135\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output.png\" alt=\"Output call_by_reference.ino\" class=\"wp-image-16336\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output-300x47.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output-768x120.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_reference_output-860x135.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output call_by_reference.ino<\/figcaption><\/figure>\n<\/div>\n<p>The function uses the original with a different identifier. Accordingly, the variable remains changed after returning from the function.<\/p>\r\n<p>Passing variables by reference has the advantage that it is not necessary to create a local copy. This saves time and memory.<\/p>\r\n\n<h4 class=\"wp-block-heading\" id=\"too_many_return_values\">Too many return values?<\/h4>\n\n<p>There is another advantage. Sometimes you want to process multiple variables in a function and use the results after returning from the function. Passing and processing are unproblematic. However, in C++ you can only return one value. We get around this problem by passing the variables by reference.<\/p>\r\n<p>Here is a small example:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"changing_several_variables.ino\" data-enlighter-title=\"changing_several_variables.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  int iVar_1 = 42;\r\n  int iVar_2 = 4242;\r\n  Serial.println(\"Original values:\");\r\n  Serial.print(\"iVar_1: \"); Serial.println(iVar_1);\r\n  Serial.print(\"iVar_2: \"); Serial.println(iVar_2);\r\n  swap(iVar_1, iVar_2);\r\n  Serial.println(\"Swapped values:\");\r\n  Serial.print(\"iVar_1: \"); Serial.println(iVar_1);\r\n  Serial.print(\"iVar_2: \"); Serial.println(iVar_2); \r\n}\r\n\r\nvoid loop() {\/*&nbsp;empty&nbsp;*\/}\r\n\r\nvoid swap(int &amp;val_1, int &amp;val_2){\r\n  int temp = val_1;\r\n  val_1 = val_2;\r\n  val_2 = temp;  \r\n}<\/pre>\r\n\n<p>The sketch swaps the values of two variables. This would not be possible with call-by-value. Here is the output:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"205\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output.png\" alt=\"Output changing_several_variables.ino\" class=\"wp-image-16345\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output-300x71.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output-768x182.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/changing_several_variables_output-860x205.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output changing_several_variables.ino<\/figcaption><\/figure>\n<\/div>\n<p>If you want to pass a parameter by reference, but do not want to change it in the function, then it is a good idea to prefix the parameter with the keyword <code>const<\/code> in the receiving function. This makes the code clearer and protects you from your own mistakes.<\/p>\r\n\n<h3 class=\"wp-block-heading\" id=\"call_by_pointer\">&#8220;Call-by-Pointer&#8221;<\/h3>\n\n<p>Alternatively, you can work in the function with a pointer to the variable to be edited. This should then be called &#8220;call-by-pointer&#8221;. <strong>However, since the term is unusual<\/strong>, I have put quotation marks around it.<\/p>\r\n<p>When you call the function, you prefix the parameter with the address operator. In the receiving function, the indirection operator is used:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"call_by_pointer.ino\" data-enlighter-title=\"call_by_pointer.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  int iVar = 42;\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar);\r\n  passAsPointer(&amp;iVar);\r\n  Serial.print(\"iVar in Setup: \"); Serial.println(iVar); \r\n}\r\n\r\nvoid loop() {\/*&nbsp;empty&nbsp;*\/}\r\n\r\nvoid passAsPointer(int *var){\r\n  *var *= 2;\r\n  Serial.print(\"Variable in passAsPointer(): \"); Serial.println(*var);  \r\n}<\/pre>\r\n\n<p>Here, too, we work with the original variable within the function. Therefore, the variable is changed permanently:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"135\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output.png\" alt=\"\" class=\"wp-image-16338\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output-300x47.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output-768x120.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/call_by_pointer_output-860x135.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output call-by-pointer.ino<\/figcaption><\/figure>\n<\/div>\n<p>I would generally prefer the call-by-reference method. However, when passing arrays, we are automatically using the &#8220;call-by-pointer&#8221; method.<\/p>\r\n\n<h4 class=\"wp-block-heading\" id=\"passing_objects\">Passing objects<\/h4>\n\n<p>If you pass objects as pointers and use object methods in the function, then you must use the dereference operator within the function or replace the point operator with the arrow operator. Here is an example:<\/p>\r\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"using_object_methods_in_functions.ino\" data-enlighter-title=\"using_object_methods_in_functions.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  String string = \"Hello world\";\r\n  Serial.print(\"string in setup: \"); Serial.println(string);\r\n  upperCase(&amp;string);\r\n  Serial.print(\"string in setup: \"); Serial.println(string);\r\n}\r\n\r\nvoid loop() {\/* empty *\/}\r\n\r\nvoid upperCase(String *localString){\r\n  Serial.print(\"Upper case: \");\r\n  localString-&gt;toUpperCase(); \/\/ alternative: *localString.toUpperCase;\r\n  Serial.println(*localString); \r\n}<\/pre>\r\n\n<p>And here is the output:<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function.png\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"143\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function.png\" alt=\"Output using_object_methods_in_functions.ino\" class=\"wp-image-16348\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function-300x50.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function-768x127.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/using_class_methods_in_function-860x143.png 860w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption class=\"wp-element-caption\">Output using_object_methods_in_functions.ino<\/figcaption><\/figure>\n<\/div>\n<h3 class=\"wp-block-heading\" id=\"passing_arrays\">Passing arrays<\/h3>\n\n<p>You pass an array to a function using the name of the array as a parameter. With this, you do not pass the whole array, but only the pointer to the array. A copy of the pointer is then created in the function. As we saw earlier, the information about the size of the array, i.e. the number of elements, is lost in the process. You can conveniently calculate the size of the array via <code>sizeof(anyArray)\/sizeof(datatype)<\/code> and pass it as an additional parameter.<\/p>\r\n\n<div class=\"scroll-paragraph\">\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"pass_array.ino\" data-enlighter-title=\"pass_array.ino\">void setup() {\r\n  Serial.begin(9600);\r\n  int intArray[4] = {0};\r\n  for(int i=0; i&lt;4; i++){\r\n    intArray[i] = i * 10;\r\n  }\r\n  \r\n  Serial.println(\"Array in setup():\");\r\n  for(int i=0; i&lt;4; i++){\r\n    Serial.print(intArray[i]); \r\n    Serial.print(\"  \");\r\n  }\r\n  Serial.println();\r\n  Serial.println();\r\n  \r\n  doubleArray(intArray, sizeof(intArray)\/sizeof(int));\r\n  \r\n  Serial.println(\"Array in setup():\");\r\n  for(int i=0; i&lt;4; i++){\r\n    Serial.print(intArray[i]); \r\n    Serial.print(\"  \");\r\n  }\r\n  Serial.println();\r\n  Serial.println();\r\n}\r\n\r\nvoid loop() {\/*&nbsp;empty&nbsp;*\/}\r\n\r\nvoid doubleArray(int *arr, size_t count){\r\n  Serial.println(\"Array in doubleArray():\");\r\n  for(unsigned int i=0; i&lt;count; i++){\r\n    arr[i] *= 2;\r\n    Serial.print(arr[i]); Serial.print(\"  \");\r\n  }\r\n  Serial.println();\r\n  Serial.println();\r\n}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<\/div>\r\n\n<p>Since the function works with the original, the change made by the function is permanent.<\/p>\r\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output.png\" alt=\"Output pass_array.ino\" class=\"wp-image-16341\" width=\"840\" height=\"239\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output.png 864w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output-300x85.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output-768x219.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/pass_array_output-860x246.png 860w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/a><figcaption class=\"wp-element-caption\">Output pass_array.ino<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">Closing words<\/h2>\n\n<p>I think that with this post I have covered the most important aspects on the subject of pointers and references. But more important is what you think. So if there are any open questions or things are unclear or misunderstood, feel free to let me know. Otherwise, I hope I haven&#8217;t completely confused you with this post.<\/p>\r\n\n<h2 class=\"wp-block-heading\">Acknowledgement<\/h2>\n\n<p>I owe the post image to <a href=\"https:\/\/pixabay.com\/de\/users\/pencilparker-7519217\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3045123\" target=\"_blank\" rel=\"noopener\">pencil parker<\/a> on <a href=\"https:\/\/pixabay.com\/de\/\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3045123\" target=\"_blank\" rel=\"noopener\">Pixabay<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.<\/p>\n","protected":false},"author":1,"featured_media":16307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[575],"tags":[1990,556,1987,1989,1998,1996,1993,1994,1995,1986,1984,1999,1991],"class_list":["post-17151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-and-tools","tag-address-operator","tag-arduino-en-2","tag-array-en","tag-c-array-en-2","tag-call-by-reference-en-2","tag-call-by-value-en","tag-dereference-operator-en","tag-indirection-operator","tag-parameter-passing","tag-pointer-en","tag-pointers","tag-reading-an-area-of-memory","tag-reference-operator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pointers and references &#8226; Wolles Elektronikkiste<\/title>\n<meta name=\"description\" content=\"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pointers and references &#8226; Wolles Elektronikkiste\" \/>\n<meta property=\"og:description\" content=\"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references\" \/>\n<meta property=\"og:site_name\" content=\"Wolles Elektronikkiste\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-13T20:12:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-25T15:18:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1272\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Wolfgang Ewald\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wolfgang Ewald\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references\"},\"author\":{\"name\":\"Wolfgang Ewald\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"headline\":\"Pointers and references\",\"datePublished\":\"2023-01-13T20:12:44+00:00\",\"dateModified\":\"2025-01-25T15:18:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references\"},\"wordCount\":2186,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Zeiger.jpg\",\"keywords\":[\"address operator\",\"Arduino\",\"array\",\"C array\",\"call-by-reference\",\"call-by-value\",\"dereference operator\",\"indirection operator\",\"parameter passing\",\"pointer\",\"pointers\",\"reading an area of memory\",\"reference operator\"],\"articleSection\":[\"Software and tools\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references\",\"name\":\"Pointers and references &#8226; Wolles Elektronikkiste\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Zeiger.jpg\",\"datePublished\":\"2023-01-13T20:12:44+00:00\",\"dateModified\":\"2025-01-25T15:18:26+00:00\",\"description\":\"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#primaryimage\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Zeiger.jpg\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Zeiger.jpg\",\"width\":1272,\"height\":1200},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/pointers-and-references#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pointers and references\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\",\"name\":\"Wolles Elektronikkiste\",\"description\":\"Die wunderbare Welt der Elektronik\",\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\",\"name\":\"Wolfgang Ewald\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"width\":512,\"height\":512,\"caption\":\"Wolfgang Ewald\"},\"logo\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pointers and references &#8226; Wolles Elektronikkiste","description":"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references","og_locale":"en_US","og_type":"article","og_title":"Pointers and references &#8226; Wolles Elektronikkiste","og_description":"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.","og_url":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references","og_site_name":"Wolles Elektronikkiste","article_published_time":"2023-01-13T20:12:44+00:00","article_modified_time":"2025-01-25T15:18:26+00:00","og_image":[{"width":1272,"height":1200,"url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg","type":"image\/jpeg"}],"author":"Wolfgang Ewald","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wolfgang Ewald","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#article","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references"},"author":{"name":"Wolfgang Ewald","@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"headline":"Pointers and references","datePublished":"2023-01-13T20:12:44+00:00","dateModified":"2025-01-25T15:18:26+00:00","mainEntityOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references"},"wordCount":2186,"commentCount":0,"publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg","keywords":["address operator","Arduino","array","C array","call-by-reference","call-by-value","dereference operator","indirection operator","parameter passing","pointer","pointers","reading an area of memory","reference operator"],"articleSection":["Software and tools"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references","url":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references","name":"Pointers and references &#8226; Wolles Elektronikkiste","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#website"},"primaryImageOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#primaryimage"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg","datePublished":"2023-01-13T20:12:44+00:00","dateModified":"2025-01-25T15:18:26+00:00","description":"Pointers and references are very useful, but equally confusing, especially for beginners. This is an attempt to bring some light into the darkness.","breadcrumb":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#primaryimage","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2022\/10\/Zeiger.jpg","width":1272,"height":1200},{"@type":"BreadcrumbList","@id":"https:\/\/wolles-elektronikkiste.de\/en\/pointers-and-references#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/wolles-elektronikkiste.de\/en"},{"@type":"ListItem","position":2,"name":"Pointers and references"}]},{"@type":"WebSite","@id":"https:\/\/wolles-elektronikkiste.de\/en#website","url":"https:\/\/wolles-elektronikkiste.de\/en","name":"Wolles Elektronikkiste","description":"Die wunderbare Welt der Elektronik","publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wolles-elektronikkiste.de\/en?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46","name":"Wolfgang Ewald","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","width":512,"height":512,"caption":"Wolfgang Ewald"},"logo":{"@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/17151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/comments?post=17151"}],"version-history":[{"count":2,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/17151\/revisions"}],"predecessor-version":[{"id":23042,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/17151\/revisions\/23042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media\/16307"}],"wp:attachment":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media?parent=17151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/categories?post=17151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/tags?post=17151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}