{"id":3620,"date":"2014-12-18T12:23:30","date_gmt":"2014-12-18T20:23:30","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=3620"},"modified":"2024-12-28T11:44:34","modified_gmt":"2024-12-28T19:44:34","slug":"what-are-pointers","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/what-are-pointers\/","title":{"rendered":"What is a Pointer?"},"content":{"rendered":"<p>This blog post will cover the basics of <em>pointers<\/em>, a programming tool that is used in languages like\u00a0C and C++. In this post, we will be using C as our primary language. <em>Pointers <\/em>are variables that contain a <em><a title=\"memory address\" href=\"http:\/\/en.wikipedia.org\/wiki\/Memory_address\" target=\"_blank\" rel=\"noopener\">memory address<\/a>\u00a0<\/em>(a concept used to access the computer&#8217;s primary storage memory). Variables normally contain a value such as 1 or \u2018a\u2019, but pointers contain an address of the value. When we reference a variable through pointers, this is called <em>indirection<\/em>. Each link goes to a text file of C code. This code can be run as is and will help show us the power of pointers!<\/p>\n<p><!--more--><br \/>\n<!--more--><\/p>\n<p>The \u201c&amp;\u201d (ampersand) operator is the address operator, using the ampersand will return the address of a variable.<br \/>\n<!--more--><\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3895\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a.png\" alt=\"a\" width=\"517\" height=\"215\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a.png 517w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/a-225x93.png 225w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/printaddr.txt\">Print Address Code<\/a><br \/>\n<!--more--><br \/>\n<!--more--><\/p>\n<p>The \u201c*\u201d (asterisk) operator is a pointer to a variable.<br \/>\n<!--more--><\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3917\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1-600x188.png\" alt=\"b\" width=\"600\" height=\"188\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1-600x189.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1-225x70.png 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1.png 754w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/address.txt\">Compare Address&#8217; Code<\/a><\/p>\n<p><!--more--><br \/>\n<!--more--><br \/>\nUsing pointers allows us to pass variables into functions, and then manipulate the values of these variables inside the function, and have them persist after the function is out of scope. Without pointers, this is impossible!<\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3910\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c.png\" alt=\"c\" width=\"587\" height=\"439\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c.png 587w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/c-225x168.png 225w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/addone.txt\">Add One Code<\/a><br \/>\n<!--more--><br \/>\n<!--more--><\/p>\n<p>Pointers are powerful! To finish this blog post, we will see how pointers can be used to sort an array of numbers with the \u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Bubble_sort\" target=\"_blank\" rel=\"noopener\">bubble sort<\/a>\u00a0function (a type of sorting algorithm).<br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/d.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3912\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/d-311x600.png\" alt=\"d\" width=\"311\" height=\"600\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/d-311x600.png 311w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/d-116x225.png 116w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/d.png 432w\" sizes=\"auto, (max-width: 311px) 100vw, 311px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/bubblesort.txt\">Bubblesort Code<\/a><br \/>\n<!--more--><br \/>\n<!--more--><\/p>\n<p>With pointers, we can change the entire array in bubblesort, and these changes will remain persistent even after the bubblesort function is no longer in scope!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-3620 jlk' data-task='like' data-post_id='3620' data-nonce='8896bc70a6' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-3620 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-3620 jlk' data-task='unlike' data-post_id='3620' data-nonce='8896bc70a6' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-3620 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-3620 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>This blog post will cover the basics of pointers, a programming tool that is used in languages like C and C++. In this post, we will be using C as our primary language. Pointers are variables that contain a memory address (a concept used to access the computer&#8217;s primary storage memory). Variables normally contain a value such as 1 or \u2018a\u2019, but pointers contain an address of the value. When we reference a variable through pointers, this is called indirection. Each link goes to a text file of C code. This code can be run as is and will help show us the power of pointers!<\/p>\n","protected":false},"author":29,"featured_media":3917,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4478],"class_list":["post-3620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/b1.png","jetpack_sharing_enabled":true,"authors":[{"term_id":4478,"user_id":29,"is_guest":0,"slug":"josh-woldstad","display_name":"Josh","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/166ab2302d3f5f5f33ecab52f79a1ab44277f4ef8f5073fe66cf54070f6cd419?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=3620"}],"version-history":[{"count":1,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3620\/revisions"}],"predecessor-version":[{"id":31159,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/3620\/revisions\/31159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/3917"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=3620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=3620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=3620"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=3620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}