{"id":157,"date":"2017-09-13T10:39:40","date_gmt":"2017-09-13T13:39:40","guid":{"rendered":"http:\/\/www.professores.uff.br\/diomarcesarlobao\/?page_id=157"},"modified":"2017-09-13T10:39:40","modified_gmt":"2017-09-13T13:39:40","slug":"pointers-f","status":"publish","type":"page","link":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/pointers-f\/","title":{"rendered":"pointers.f"},"content":{"rendered":"<pre>c&lt;html&gt;\r\nc&lt;head&gt;&lt;title&gt;pointers.f&lt;\/title&gt;&lt;\/head&gt;\r\nc&lt;body&gt;\r\nc&lt;pre&gt;\r\n\r\n      program pointer_test\r\n      implicit none\r\nc\r\nc   Demonstrate use of pointers to manipulate sections of arrays\r\nc   Notice that once associated, p2 and p1 are treated like normal\r\nc   arrays in the program\r\nc\r\nc    John Mahaffy 4\/17\/96\r\nc\r\nc&lt;a name=\"target\"&gt;&lt;font color=\"FF0000\"&gt;\r\n      real, target :: a(10,10)\r\nc&lt;\/font&gt;&lt;\/a&gt;\r\nc&lt;a name=\"point\"&gt;&lt;font color=\"FF0000\"&gt;\r\n      real, pointer :: p2(:,:), p1(:)\r\nc&lt;\/font&gt;&lt;\/a&gt;\r\n      integer iub, i, j\r\nc\r\nc   Define a generic interface to permit output of information\r\nc   for either rank 1 or rank 2 arrays.  You need such an interface\r\nc   in any case to get some of the associated pointers to pass their\r\nc   contents efficiently (quietly passes a skip factor (stride).\r\nc   Without an interface our compiler allocates space and makes\r\nc   a copy of the contents of the pointer array to guarantee that the\r\nc   sequential elements in the passed array are in adjacent memory\r\nc   locations\r\nc\r\nc&lt;a name=\"int\"&gt;&lt;font color=\"FF0000\"&gt;\r\n      interface parray\r\nc&lt;\/font&gt;&lt;\/a&gt;\r\n            subroutine p2array (a,title,aname)\r\n            real a(:,:)\r\nc&lt;a name=\"char\"&gt;&lt;font color=\"FF0000\"&gt;\r\n            character*(*) title,aname\r\nc&lt;\/font&gt;&lt;\/a&gt;\r\n            end subroutine\r\n            subroutine p1array (a,title,aname)\r\n            real a(:)\r\n            character*(*) title,aname\r\n            end subroutine\r\n      end interface\r\nc\r\nc  Open a file to contain results printed to the screen\r\nc\r\n      open (9,file='pointers.out')\r\nc\r\nc    Set some initial values\r\nc\r\n      do    i=1,10\r\n         do j=1,10\r\n            a(i,j)=1000*i+j\r\n         enddo\r\n      enddo\r\nc\r\n      call parray(a, 'original array', 'a')\r\nc\r\nc   p2 becomes a rank 2 array associated with part of \"a\"\r\nc\r\n      p2 =&gt; a(3:6,7:9)\r\n      call parray (p2,'p2 =&gt; a(3:6,7:9) ','p2')\r\nc\r\nc   add 800 to all elements in p2\r\nc\r\n      p2 = -p2 - 800\r\n      call parray (p2,'results of p2 = -p2 - 800 ','p2')\r\nc\r\nc   What have we done to the array \"a\" ?\r\nc\r\n      call parray(a, 'Here''s what happened to \"a\" ', 'a')\r\nc\r\nc   Associate p1 with column 3 of \"a\"\r\nc\r\n      p1 =&gt; a(:,3)\r\n      call parray (p1, 'Associate p1 with column 3: p1 =&gt; a(:,3)','p1')\r\nc\r\nc   Modify  p1\r\nc     &lt;a name=1&gt;&lt;font color=FF0000&gt;\r\n      iub = ubound(p1,dim=1)\r\nc     &lt;\/font&gt;\r\n      do 40 i=1,iub\r\n   40 p1(i) = i**2\r\n      call parray (p1,'Modify p1 with the equation: p1(i) = i**2','p1')\r\nc\r\nc   Now associate p1 with row 8 of \"a\"\r\nc\r\n      p1 =&gt; a(8,:)\r\n      call parray (p1, 'Associate p1 with row 8:  p1 =&gt; a(8,:)','p1')\r\nc\r\nc   Modify  p1\r\nc\r\n      iub=ubound(p1,dim=1)\r\n      do 60 i=1,iub\r\n   60 p1(i) = i\r\n      call parray (p1, 'Modify p1 with the equation: p1(i) = i ','p1')\r\nc\r\nc   Now what does the array \"a\" look like ?\r\nc\r\n      call parray(a, '\"a\" after all changes to p1 and p2 ', 'a')\r\nc\r\n      stop\r\n      end\r\nc=======================================================================\r\n      subroutine p2array (a,title,aname)\r\n      implicit none\r\nc\r\nc    Print out a 2-D array with header information\r\nc\r\nc    John Mahaffy 4\/17\/96\r\nc\r\nc   INPUT arguments\r\nc\r\nc   a     -   array to be printed\r\nc   title -   title information for the output\r\nc   aname -   name of the array being printed\r\nc\r\nc\r\nc   The use of colons in the following type declaration is\r\nc   necessary so that the subroutine will look for some\r\nc   hidden arguments passed as a result of an interface statement\r\nc   in the calling routine.   These hidden arguments provide\r\nc   detailed information on the shape of the array.\r\nc\r\n      real a(:,:)\r\nc\r\n      character*(*) title,aname\r\n      character*(4) clb1,cub1,clb2,cub2\r\n      character*32 info\r\n      integer  lb1, ub1, lb2, ub2, i\r\nc\r\nc   Build a header giving the array name and shape\r\nc     &lt;a name=2&gt;&lt;font color=FF0000&gt;\r\n      lb1 = lbound(a ,dim=1)\r\nc     &lt;\/font&gt;\r\n      ub1 = ubound(a ,dim=1)\r\n      lb2 = lbound(a ,dim=2)\r\n      ub2 = ubound (a ,dim=2)\r\n      write(clb1,2001) lb1\r\n      write(cub1,2001) ub1\r\n      write(clb2,2001) lb2\r\n      write(cub2,2001) ub2\r\nc\r\n      info= aname\/\/'('\/\/trim(adjustl(clb1))\/\/':'\r\n     &amp;      \/\/trim(adjustl(cub1))\/\/','\r\n     &amp;      \/\/trim(adjustl(clb2))\/\/':'\r\n     &amp;      \/\/trim(adjustl(cub2))\/\/') ='\r\nc\r\n      write(*,2000) repeat('=',80)\r\n      write(*,2000) title\r\n      write(*,2000) repeat('-',80)\r\n      write(*,2000) info\r\nc\r\n      write(9,2000) repeat('=',80)\r\n      write(9,2000) title\r\n      write(9,2000) repeat('-',80)\r\n      write(9,2000) info\r\nc\r\n      do i = lb1,ub1\r\n         write(*,2002) i, a(i,:)\r\n         write( 9,2002) i, a(i,:)\r\n      enddo\r\n 2000 format (a)\r\n 2001 format (i4)\r\n 2002 format('row',i4,10f7.0,(7x,10f7.0))\r\n      end\r\nc=======================================================================\r\n      subroutine p1array (a,title,aname)\r\n      implicit none\r\nc\r\nc    Print out a 1-D array with header information\r\nc\r\nc\r\nc    John Mahaffy 4\/17\/96\r\nc\r\nc   INPUT arguments\r\nc\r\nc   a     -   array to be printed\r\nc   title -   title information for the output\r\nc   aname -   name of the array being printed\r\nc\r\nc\r\nc   The use of colon  in the following type declaration is\r\nc   necessary so that the subroutine will look for some\r\nc   hidden arguments passed as a result of an interface statement\r\nc   in the calling routine. These hidden arguments provide\r\nc   detailed information on the shape of the array\r\nc\r\n      real a(:)\r\nc\r\n      character*(*) title,aname\r\n      character*(4) clb1,cub1\r\n      character*32 info\r\n      integer  lb1, ub1\r\nc\r\nc   Build a header giving the array name and shape\r\nc\r\n      lb1 = lbound(a ,dim=1)\r\n      ub1 = ubound(a ,dim=1)\r\n      write(clb1,2001) lb1\r\n      write(cub1,2001) ub1\r\nc\r\n      info= aname\/\/'('\/\/trim(adjustl(clb1))\/\/':'\r\n     &amp;      \/\/trim(adjustl(cub1))\/\/')  '\r\nc\r\n      write(*,2000) repeat('=',80)\r\n      write(*,2000) title\r\n      write(*,2000) repeat('-',80)\r\n      write(*,2000) info\r\nc\r\n      write(9,2000) repeat('=',80)\r\n      write(9,2000) title\r\n      write(9,2000) repeat('-',80)\r\n      write(9,2000) info\r\nc\r\n         write(*,2002)  a\r\n         write( 9,2002)  a\r\n 2000 format (a)\r\n 2001 format (i4)\r\n 2002 format(10f7.0)\r\n      end\r\nc&lt;\/pre&gt;\r\nc&lt;\/body&gt;\r\nc&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>c&lt;html&gt; c&lt;head&gt;&lt;title&gt;pointers.f&lt;\/title&gt;&lt;\/head&gt; c&lt;body&gt; c&lt;pre&gt; program pointer_test implicit none c c Demonstrate use of pointers to manipulate sections of arrays c Notice that once associated, p2 and p1 are treated like normal c arrays in the program c c John Mahaffy 4\/17\/96 c c&lt;a name=&#8221;target&#8221;&gt;&lt;font color=&#8221;FF0000&#8243;&gt; real, target :: a(10,10) c&lt;\/font&gt;&lt;\/a&gt; c&lt;a name=&#8221;point&#8221;&gt;&lt;font color=&#8221;FF0000&#8243;&gt; real, pointer [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-157","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":1,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/157\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/pages\/157\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.professores.uff.br\/diomarcesarlobao\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}