{"id":3065,"date":"2018-04-28T00:01:15","date_gmt":"2018-04-28T07:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3065"},"modified":"2018-05-05T08:22:33","modified_gmt":"2018-05-05T15:22:33","slug":"peeking-and-poking-with-pointers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3065","title":{"rendered":"Peeking and Poking with Pointers"},"content":{"rendered":"<p>Back in the microcomputer days, hobbyists used the BASIC language to program their proto-PCs. BASIC is easy to learn, sloppy and forgiving. It also hints at some low-level language attributes, primarily with the PEEK and POKE commands.<br \/>\n<!--more--><br \/>\nThe PEEK command returned the byte value at a memory location.<\/p>\n<p>The POKE command allowed you to set the value of a memory location.<\/p>\n<p>These commands should terrify you now, but in the realm of a single-user, single-tasking computer, they were fun. For the Apple II, a poster-sized sheet of &#8220;Peeks and Pokes&#8221; (<a href=\"http:\/\/beagle.applearchives.com\/Posters\/Poster%202.pdf\" rel=\"noopener\" target=\"_blank\">PDF<\/a>) was available. Use the values and BASIC commands listed to alter your computer&#8217;s function, change default settings, and generally have fun.<\/p>\n<p>On a modern computer, memory is protected &mdash; specifically to avoid malicious programs or curious programmers from altering a balanced and busy system. Yet, the C language has equivalents to the old <em>peek<\/em> and <em>poke<\/em> commands. You know them as pointers.<\/p>\n<p>For example, if you were running an Apple II and wanted to click the speaker, in BASIC you&#8217;d type <code>PEEK($C030)<\/code>. The C language equivalent might be:<\/p>\n<pre class=\"screen\">\r\nchar *p,a;\r\n\r\np = (char *)0xc030;\r\na = *p;<\/pre>\n<p>Above, <code>p<\/code> is a <em>char<\/em> pointer and it&#8217;s set to the direct memory value 0xc030. This statement is illegal in current implementations of C, as you can only assign a pointer to a compatible variable. But I remember doing similar coding back in the 1980s. Then, to access that &#8220;peek&#8221; location, you assign the value stored at <code>p<\/code> to <em>char<\/em> variable <code>a<\/code>. This approach might work, and if anyone has a runnning Apple II with a C compiler, they could confirm it. The odds of that happening are rather low.<\/p>\n<p>Today, the peeking and poking you do with pointers is limited to the realm of memory assigned to your program. (The operating system&#8217;s API might have a method to access other memory locations, but such a discussion is beyond the scope of this post.)<\/p>\n<p>In Code:Blocks, start a new Project, Console Application, C language.<\/p>\n<p>Give the project a title, <strong>Peeks and Pokes<\/strong>. And check the box to Create Debug Configuration. You can leave the Release Configuration box unchecked.<\/p>\n<p>Type the following code for <code>main.c<\/code>:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    short *p;\r\n    short a;\r\n\r\n    a = 65;\r\n    p = &amp;a;\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code generates no output, which is why you don&#8217;t need the <code>stdio.h<\/code> header file. Instead, this code is meant to be debugged, which shows you (peeks) at what&#8217;s going on in memory.<\/p>\n<p>Build and run the code. You can ignore any unused variable warnings.<\/p>\n<p>Now you debug:<\/p>\n<p>Click Line 5 to place the cursor at that location. On the Debugger toolbar, shown in Figure 1, click the Run to Cursor button outlined in blue in the figure. If you don&#8217;t see the Debugger toolbar, choose View, Toolbars, Debugger.<\/p>\n<div id=\"attachment_3066\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3066\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig1_debugging-toolbar.png\" alt=\"\" width=\"400\" height=\"80\" class=\"size-full wp-image-3066\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig1_debugging-toolbar.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig1_debugging-toolbar-300x60.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-3066\" class=\"wp-caption-text\">Figure 1. Code:Blocks&#8217; Debugging toolbar.<\/p><\/div>\n<p>After you click the Run to Cursor button, you see a flurry of activity. Click the Debugging Windows button, outlined in green in Figure 1, and choose Watches from the menu. You see the Watches window appear, which lists the code&#8217;s variables, similar to what&#8217;s shown in Figure 2.<\/p>\n<div id=\"attachment_3067\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3067\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig2_watches.png\" alt=\"\" width=\"400\" height=\"286\" class=\"size-full wp-image-3067\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig2_watches.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/04\/0428-fig2_watches-300x215.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-3067\" class=\"wp-caption-text\">Figure 2. The Watches window shows you variables used in your code.<\/p><\/div>\n<p>The first column shows the code&#8217;s variable names. The second column shows their values. At this point in the code (Line 5), the variables are uninitialized. The values shown are whatever garbage already exists in memory. And, yes, this garbage is why it&#8217;s important in C to always initialize your variables.<\/p>\n<p>On the Debugging toolbar, click the Next Line button, which is just to the right of the Run to Cursor button on the Debugging toolbar.<\/p>\n<p>You see the value for variable <code>a<\/code> set to 65.<\/p>\n<p>When you click the Next Line button again, you see the memory location of variable <code>a<\/code> assigned to pointer <code>p<\/code>. On my screen, the value is 0x60ff0a. At that precise memory location in my computer, the <em>short int<\/em> value 65 sits and waits.<\/p>\n<p>The program is over, co click the red X button on the Debugging toolbar to exit the debugger and stop.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3075\">next week&#8217;s Lesson<\/a>, I&#8217;ll show you a memory map where you can view the value and marvel at its brilliance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pointers reveal the contents of memory locations, similar to BASIC commands from years ago. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3065\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3065","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3065"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3065\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3065\/revisions\/3094"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}