{"id":8046,"date":"2023-01-29T11:34:33","date_gmt":"2023-01-29T09:34:33","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=8046"},"modified":"2026-02-06T18:40:58","modified_gmt":"2026-02-06T16:40:58","slug":"pr2en1-pointers","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/","title":{"rendered":"PR2EN1: Pointers"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Lab_materials\" >Lab materials<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Reminder_on_style\" >Reminder on style<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Tasks\" >Tasks<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Lab_task_1_W01-1_Pointer_to_a_variable\" >Lab task 1 [W01-1]: Pointer to a variable<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Theory_nugget_1_stderr_standard_stream\" >Theory nugget 1: stderr standard stream<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Theory_nugget_New_macros_introduced_in_the_starter_code\" >Theory nugget: New macros introduced in the starter code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Testing\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Lab_task_2_part_1_W01-2_Pointer_to_an_array\" >Lab task 2 part 1 [W01-2]: Pointer to an array<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Requirements-2\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Testing-2\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Lab_task_2_part_2_W01-3_Interpretation_of_array_concept\" >Lab task 2 part 2 [W01-3]: Interpretation of array concept<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Requirements-3\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Testing-3\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Extra_task_W01-4_MinMax_task_with_addresses\" >Extra task [W01-4]: MinMax task with addresses<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Requirements-4\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Testing-4\" >Testing<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#After_the_class_you_should\" >After the class, you should<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_materials\"><\/span><strong>Lab materials<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/slaidid-en\/00_introduction.pdf\"><strong>Introduction<\/strong><\/a><\/li>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/slaidid-en\/01_pointers.pdf\"><strong>Pointers<\/strong><\/a><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Reminder_on_style\"><\/span>Reminder on style<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Coding style must be uniform throughout all the code files<\/li>\n<li>Your code should be easily readable and maintainable<\/li>\n<li>Prefer simple over bloated and overengineered code<\/li>\n<li>Divide your code into functions\n<ul>\n<li>Functions should be simple, short, do only one thing and do it well<\/li>\n<li>Keep computational and input\/output functions separate. If a function produces a result, it should avoid side effects if possible.<\/li>\n<li>Functions should be easily reusable. This will help you during the test and the exam.<\/li>\n<li>main function should be the first function in a file. All user-defined functions should be positioned after the main function.<\/li>\n<\/ul>\n<\/li>\n<li>Use reasonably fast algorithms within the ones we&#8217;ve studies so far<\/li>\n<li>Avoid global variables in most situations, there are a few where it&#8217;s okay and those situations will be mentioned separately during the course.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Tasks\"><\/span><span id=\"Tasks\" class=\"ez-toc-section\"><\/span>Tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>There are two lab tasks in this class. The first task focuses on passing data using pointers and the second task is about how arrays and pointers work together. Second task is divided into two parts and extended by an extra task.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_1_W01-1_Pointer_to_a_variable\"><\/span>Lab task 1 [W01-1]: Pointer to a variable<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>This is a classical swap the two values in a function task. Try to solve this task by instructions and slides alone, without using outside help or materials.<\/p>\n<p>Download the starter code here: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/aluskoodid\/t1_swap_template.c\">https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/aluskoodid\/t1_swap_template.c<\/a><\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Theory_nugget_1_stderr_standard_stream\"><\/span>Theory nugget 1: stderr standard stream<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>This semester we will start to separate error outputs from normal outputs. All error outputs will go to stderr standard error stream. This is common for most programming languages, including C. It allows to filter out errors from the normal output, look up &#8220;redirect stderr to file&#8221; for more information.<\/p>\n<p>Old way: print all errors to <span class=\"lang:c highlight:0 decode:true crayon-inline\">stdout<\/span>\u00a0 standard output stream: <span class=\"lang:c decode:true crayon-inline\">printf(&#8220;Error! &#8230;.&#8221;);<\/span><\/p>\n<p>From now on: print all errors to <span class=\"lang:c highlight:0 decode:true crayon-inline\">stderr<\/span>\u00a0 standard error stream: <span class=\"lang:c decode:true crayon-inline\">fprintf(stderr, &#8220;Error! &#8230;&#8221;);<\/span><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Theory_nugget_New_macros_introduced_in_the_starter_code\"><\/span>Theory nugget: New macros introduced in the starter code<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>When reading through the starter code, you&#8217;ll notice a new element\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline\">__FUNCTION__<\/span>\u00a0 &#8211; this is a string that prints the function name where it is used. It will be resolved when you compile the code &#8211; do not attempt to modify it. Starting from C99 standard, you can also use\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline \">__func__<\/span>\u00a0 &#8211; both achieve the same thing.<\/p>\n<p>In addition, there are two more macros that are often used for debugging code:<\/p>\n<ul>\n<li><span class=\"lang:c highlight:0 decode:true crayon-inline\">__LINE__<\/span> prints the line number where the macro is used<\/li>\n<li><span class=\"lang:c highlight:0 decode:true crayon-inline\">__FILE__<\/span> prints the name of the file where the macro is used<\/li>\n<\/ul>\n<p>Using those, we could write something like<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">fprintf(stderr, \"Error: Unexpected NULL-ptr in function %s at %s:%d\\n\", \r\n        __func__, __FILE__, __LINE__);<\/pre>\n<p>This makes it easier to observe in which file, in which function and on which line an error occurred.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Use the starter code provided<\/li>\n<li>All parts you need to complete are marked with <span class=\"lang:c highlight:0 decode:true crayon-inline \">TODO<\/span>\u00a0 in the starter code<\/li>\n<li>Create two functions according to function comments in the starter code. One to read values, second to swap values. Call the functions.<\/li>\n<li>Print out the memory addresses where the data is stored in three different locations\n<ol>\n<li>In the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function, show where the variables are declared at in memory<\/li>\n<li>In the reading function, show the addresses where you are storing the values to<\/li>\n<li>In the swap function, show the location where the values are you are swapping<\/li>\n<\/ol>\n<\/li>\n<li><strong>Explain the significance of the address values when defending!<\/strong><\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto@risto-lt3:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/pointers$ .\/t1_swap\r\nAddresses in function main\r\nx @ 0x7ffffd82f7d0\r\ny @ 0x7ffffd82f7d4\r\n\r\n\r\nAddresses in function ReadValues\r\nx @ 0x7ffffd82f7d0\r\ny @ 0x7ffffd82f7d4\r\n\r\nEnter value x: 19\r\nEnter value y: 4\r\n\r\nOriginal values in main\r\nx = 19\r\ny = 4\r\n\r\nAddresses in function SwapValues\r\nx @ 0x7ffffd82f7d0\r\ny @ 0x7ffffd82f7d4\r\n\r\nUpdated values in main\r\nx = 4\r\ny = 19\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_2_part_1_W01-2_Pointer_to_an_array\"><\/span>Lab task 2 part 1 [W01-2]: Pointer to an array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The purpose of the second task is to show how arrays and pointers are related to each other. In addition, we will explore pointer arithmetic and and do a reminder from last semester, where we learned about files and command line arguments.<\/p>\n<p>The program needs a data file that contains integers. Create the data file yourself! Numbers can either be separated by spaces or newlines.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-2\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Create a program that fulfills the following requirements<\/p>\n<ul>\n<li>Reads 10 integers from a file and stores them into an array<\/li>\n<li>The name of the file is read as a command line argument<\/li>\n<li>Show the array base address in the main function<\/li>\n<li>Print out the read integers and their memory addresses<\/li>\n<li>Find the minimum and maximum value from the array<\/li>\n<\/ul>\n<p>Additional requirements that must be met<\/p>\n<ul>\n<li>During this task, you are not permitted to use square brackets <span class=\"lang:c highlight:0 decode:true crayon-inline \">[]<\/span>\u00a0 for indexing the array (limitation does not apply to declaring an array). Use pointer arithmetic in all functions<\/li>\n<li>The program is halted if the file doesn&#8217;t exist or it was unable to read integers from the file &#8211; i.e. file contains something else<\/li>\n<li>You must complete the list of functions specified in the requirements<\/li>\n<li>All functions must be called from the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function<\/li>\n<li>The minimum and maximum value must be printed in the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function<\/li>\n<li>All error messages must be printed to the <span class=\"lang:c highlight:0 decode:true crayon-inline \">stderr<\/span>\u00a0 standard stream.<\/li>\n<\/ul>\n<p>Create the following functions<\/p>\n<ul>\n<li>Create a function, that reads 10 numbers\u00a0 from a file and stores them in an array.\n<ul>\n<li>Optional: if you wish, you can print out the addresses during reading for self-checking and comparison against other functions<\/li>\n<li>Parameters: array, the array size (limit), name of input file<\/li>\n<li>Return: how many numbers were read<\/li>\n<li>You must avoid reading more numbers than can fit into the array<\/li>\n<li>You can use the following partial solution for the function and fill in the gaps highlighted by TODO. It should be relatively similar to week 8 in programming one, but with the addition of the file\n<pre class=\"lang:c decode:true\">int ReadValuesFromFile(int *arr, int arrayMaxSize, const char *fileName)\r\n{\r\n    \/\/ TODO: Open the file and check if it opened\r\n    \r\n    \/\/ Read a maximum of arrayMaxSize integers\r\n    int i = 0;\r\n    while (i &lt; arrayMaxSize)\r\n    {\r\n        \/\/ TODO: Read a number from file and store the return value for checking\r\n        int ret = fscanf();\r\n        \r\n        \/\/ Something happened - either file ended or input format was incorrect\r\n        if (ret != 1)\r\n        {\r\n            \/\/ Stop reading integers and return the number of members read\r\n            break;\r\n        }\r\n        \r\n        \/\/ Increment successfully read integer count\r\n        i++;\r\n    }\r\n    \r\n    \/\/ TODO: Close the input file    \r\n    fclose();\r\n    \r\n    \/\/ TODO: Return the number of integers read\r\n    return ;\r\n}<\/pre>\n<\/li>\n<li>Additional hint: For a readable solution of calling this function, you can store the command line argument in a pointer as follows:\n<pre class=\"lang:c decode:true \">\/\/ Grab the file name\r\nchar *inputFile = argv[1];\r\n\r\n\/\/ Read data from file, \r\nint numCnt = ReadValuesFromFile(nums, LEN, inputFile);<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>Create a function that prints out all numbers and their memory addresses\n<ul>\n<li>Parameters: name of the array, number of integers read from the file<\/li>\n<\/ul>\n<\/li>\n<li>Create a function that\u00a0 find both the minimum and maximum number\n<ul>\n<li>You can only call this function once &#8211; it must find both results in one go and store them<\/li>\n<li>Function can&#8217;t return anything, i.e. the function return type must be <span class=\"lang:default highlight:0 decode:true crayon-inline \">void<\/span>\u00a0.<\/li>\n<li>Assume that only one minimum and maximum exists<\/li>\n<li>Use the concept of pointers from task 1 to solve getting multiple values from a function.\n<ul>\n<li>Do not use an array to store min\/max values!<\/li>\n<li>Do not print the results in this function.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing-2\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>When testing the base task, test for four different results<\/p>\n<ol>\n<li>Command line arguments are missing, program prints an error and closes<\/li>\n<li>File does not open, program prints an error and closes<\/li>\n<li>There are no integers in the file, program prints an error and closes<\/li>\n<li>There are some integers in the file. At most 10 will be read and printed. Minimum and maximum value will be printed.<\/li>\n<\/ol>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto@risto-lt:~\/pr2\/wk1_pointers$ .\/task2 nums.txt \r\nArray address in main(): 0x7ffd30f10220\r\n\r\nPrinting data from file\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n0x7ffd30f10240: -1\r\n0x7ffd30f10244: 7\r\n\r\nMin: -22\r\nMax: 51<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_2_part_2_W01-3_Interpretation_of_array_concept\"><\/span>Lab task 2 part 2 [W01-3]: Interpretation of array concept<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The purpose of the second part of the task is to make sure you understood the concept that an array can be interpreted as <strong>the pointer to the first member<\/strong>.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-3\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Create (or reuse \ud83d\ude09 ) a function that is able to print n numbers and their memory addresses in the array.<\/li>\n<li>The function can only have two parameters.<\/li>\n<li>The function must be built in a way that it is able to print any arbitrary array<\/li>\n<li>To demonstrate that you wrote this function correctly (and understood the topic correctly), call the function 3 times, so that\n<ul>\n<li>it will print out all members of the array<\/li>\n<li>it will print out elements 0 to 4<\/li>\n<li>it will print out elements 3 &#8211; 9<\/li>\n<\/ul>\n<\/li>\n<li>NB! What happens if you got less numbers from the file?<\/li>\n<\/ul>\n<p><strong>Hint 1:<\/strong> Think before acting! This part of the task is about the concept of pointers and arrays rather than creating some very clever and devious solution! Do not attempt to over-engineer the solution!<\/p>\n<p><strong>Hint 2:<\/strong> The text preceding the printed array members should be printed in the main() function, before the function call.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Testing-3\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Part two has two possible results<\/p>\n<ol>\n<li>Array is long enough, program will print the requested sequences<\/li>\n<li>Array does not contain enough members, program will print an error.<\/li>\n<\/ol>\n<p>The example shows expected output if enough members were read.<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto@risto-lt:~\/pr2\/wk1_pointers$ .\/task2 nums.txt \r\nArray address in main(): 0x7ffd30f10220\r\n\r\nPrinting data from file\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n0x7ffd30f10240: -1\r\n0x7ffd30f10244: 7\r\n\r\nMin: -22\r\nMax: 51\r\n\r\nArray print from 0 to 9\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n0x7ffd30f10240: -1\r\n0x7ffd30f10244: 7\r\n\r\nArray print from 0 to 4\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n\r\nArray print from 3 to 7\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Extra_task_W01-4_MinMax_task_with_addresses\"><\/span>Extra task [W01-4]: MinMax task with addresses<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In this task, we will recreate the minmax function from part 1 of task 2 . We add the ability to also print the memory addresses and indexes for the minimum and maximum value.<\/p>\n<p><strong>You are recommended to leave the original implementation in place and create a new function. <\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-4\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Create a new function to find the locations of the minimum and maximum elements in the array in one go.<\/li>\n<li>The return type of this function must be <span class=\"lang:default highlight:0 decode:true crayon-inline \">void<\/span>\u00a0.<\/li>\n<li>The function must have 4 parameters\n<ul>\n<li>Array<\/li>\n<li>Length of the array<\/li>\n<li>2 pointers or double pointers &#8211; figure out what to store in them!<\/li>\n<\/ul>\n<\/li>\n<li>In the main function, print out the following results\n<ul>\n<li>The min and max value<\/li>\n<li>The addresses of where the min and max are stored in the array<\/li>\n<li>The index of min and max value in the array.<\/li>\n<li>NB! You must calculate the results, not loop through the array<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing-4\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Expected output of extra task<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto@risto-lt:~\/pr2\/wk1_pointers$ .\/task2 nums.txt \r\nArray address in main(): 0x7ffd30f10220\r\n\r\nPrinting data from file\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n0x7ffd30f10240: -1\r\n0x7ffd30f10244: 7\r\n\r\nMin: -22\r\nMax: 51\r\n\r\nArray print from 0 to 9\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n0x7ffd30f10240: -1\r\n0x7ffd30f10244: 7\r\n\r\nArray print from 0 to 4\r\n0x7ffd30f10220: 5\r\n0x7ffd30f10224: 9\r\n0x7ffd30f10228: 15\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n\r\nArray print from 3 to 7\r\n0x7ffd30f1022c: 12\r\n0x7ffd30f10230: 8\r\n0x7ffd30f10234: 0\r\n0x7ffd30f10238: 51\r\n0x7ffd30f1023c: -22\r\n\r\nMin -22; address 0x7ffd30f1023c; index 7\r\nMax 51; address 0x7ffd30f10238; index 6<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"After_the_class_you_should\"><\/span><span id=\"After_the_class_you_should\" class=\"ez-toc-section\"><\/span><strong>After the class, you should<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Understand the requirements for this subject, including how to get a grade<\/li>\n<li>Understand what a pointer is<\/li>\n<li>Understand the significance of memory addresses<\/li>\n<li>Understand virtual and physical addresses<\/li>\n<li>Know what affects the address length<\/li>\n<li>Know the extra memory requirements for explicit use of pointers<\/li>\n<li>Be able to declare pointers<\/li>\n<li>Know the coding style aspects of declaring and using pointers<\/li>\n<li>Know what&#8217;s the importance of pointer data types<\/li>\n<li>Know the significance and use for NULL pointers<\/li>\n<li>Know the two pointer operators and where to use them<\/li>\n<li>Have some understanding of possible use cases for pointers<\/li>\n<li>Know how to use pointer arithmetic<\/li>\n<li>Have a better understanding of the scanf arguments and how pointers come into play here<\/li>\n<li>Understand why we said that arrays were editable when passed to a function, but variables were not<\/li>\n<li>Be able to pass the addresses of variables to functions as pointers<\/li>\n<li>Be able to edit multiple variables in a function<\/li>\n<li>Understand the concept of double pointers<\/li>\n<li>Know how to use a ternary operator<\/li>\n<li>Know how to use GCC macros for listing the name of the file, function and line number<\/li>\n<li>Know how and when to use stderr standard stream<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span><span id=\"Additional_content\" class=\"ez-toc-section\"><\/span><strong>Additional content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Predefined macros<br \/>\n<strong><a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/cpp\/Standard-Predefined-Macros.html\">https:\/\/gcc.gnu.org\/onlinedocs\/cpp\/Standard-Predefined-Macros.html<\/a><\/strong><\/li>\n<li>Beej\u2019s Guide to C Programming: Pointers\u2014Cower In Fear!<br \/>\n<strong><a href=\"https:\/\/beej.us\/guide\/bgc\/html\/split\/pointers.html#pointers\">https:\/\/beej.us\/guide\/bgc\/html\/split\/pointers.html#pointers<\/a><\/strong><\/li>\n<li>Beej\u2019s Guide to C Programming: Pointers II: Arithmetic<br \/>\n<strong><a href=\"https:\/\/beej.us\/guide\/bgc\/html\/split\/pointers2.html#pointers2\">https:\/\/beej.us\/guide\/bgc\/html\/split\/pointers2.html#pointers2<\/a><\/strong><\/li>\n<li>C pointers<br \/>\n<strong><a href=\"https:\/\/www.geeksforgeeks.org\/c-pointers\">https:\/\/www.geeksforgeeks.org\/c-pointers<\/a><\/strong><\/li>\n<li>Pointers &#8211; CS50 shorts<br \/>\n<strong><a href=\"https:\/\/www.youtube.com\/watch?v=XISnO2YhnsY\">https:\/\/www.youtube.com\/watch?v=XISnO2YhnsY<\/a><\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab materials Slides: Introduction Slides: Pointers Reminder on style Coding style must be uniform throughout all the code files Your code should be easily readable and maintainable Prefer simple over bloated and overengineered code Divide your code into functions Functions should be simple, short, do only one thing and do it well Keep computational and &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en1-pointers\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">PR2EN1: Pointers<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-8046","post","type-post","status-publish","format-standard","hentry","category-pr2_en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/8046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=8046"}],"version-history":[{"count":19,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/8046\/revisions"}],"predecessor-version":[{"id":11206,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/8046\/revisions\/11206"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=8046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=8046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}