From 9386b909632d4a8301d45333af1a12dbc0ea0f2e Mon Sep 17 00:00:00 2001 From: Euler-37 Date: Sun, 31 Mar 2024 11:16:34 +0000 Subject: [PATCH] deploy: cf69a501b6998568e8b6252b2e0f3a226b433a7b --- 404.html | 2 +- ch01-00-getting-started.html | 2 +- ch01-01-install-compiler.html | 2 +- ch01-02-gfortran.html | 2 +- ch01-03-fpm.html | 2 +- ch02-00-first-fortran.html | 2 +- ch02-01-program-unit.html | 2 +- ch03-00-declaration.html | 2 +- ch03-01-precision.html | 2 +- ch03-02-operation.html | 2 +- ch03-03-array.html | 2 +- ch03-04-character-array.html | 2 +- ch04-00-if.html | 2 +- ch04-01-select.html | 2 +- ch04-02-do.html | 2 +- ch04-03-do-array.html | 2 +- ch05-00-function.html | 2 +- ch05-01-subroutine.html | 2 +- ch05-02-funckeyword.html | 2 +- ch05-03-callback.html | 2 +- ch05-04-module.html | 2 +- ch05-05-overload.html | 2 +- ch06-00-file.html | 2 +- ch06-01-format.html | 2 +- ch07-00-type.html | 2 +- ch07-01-procbind.html | 2 +- ch07-02-typeoverload.html | 2 +- ch08-00-pointer.html | 2 +- ch08-01-memory.html | 2 +- ch08-02-procpointer.html | 6 +- ch08-03-pointertips.html | 244 ++++++++++++++++++++++++++++++++++ index.html | 2 +- mdbook-cheat-sheet.html | 6 +- preface.html | 2 +- print.html | 42 +++++- searchindex.js | 2 +- searchindex.json | 2 +- title-page.html | 2 +- 38 files changed, 325 insertions(+), 41 deletions(-) create mode 100644 ch08-03-pointertips.html diff --git a/404.html b/404.html index 4db8345..2b8e395 100644 --- a/404.html +++ b/404.html @@ -85,7 +85,7 @@ diff --git a/ch01-00-getting-started.html b/ch01-00-getting-started.html index 718c626..2cbfdeb 100644 --- a/ch01-00-getting-started.html +++ b/ch01-00-getting-started.html @@ -84,7 +84,7 @@ diff --git a/ch01-01-install-compiler.html b/ch01-01-install-compiler.html index c08d1f6..5dba814 100644 --- a/ch01-01-install-compiler.html +++ b/ch01-01-install-compiler.html @@ -84,7 +84,7 @@ diff --git a/ch01-02-gfortran.html b/ch01-02-gfortran.html index 761e810..c1129b9 100644 --- a/ch01-02-gfortran.html +++ b/ch01-02-gfortran.html @@ -84,7 +84,7 @@ diff --git a/ch01-03-fpm.html b/ch01-03-fpm.html index 180ce51..10ac25f 100644 --- a/ch01-03-fpm.html +++ b/ch01-03-fpm.html @@ -84,7 +84,7 @@ diff --git a/ch02-00-first-fortran.html b/ch02-00-first-fortran.html index cdd7b14..fdaef1f 100644 --- a/ch02-00-first-fortran.html +++ b/ch02-00-first-fortran.html @@ -84,7 +84,7 @@ diff --git a/ch02-01-program-unit.html b/ch02-01-program-unit.html index a8bb4e1..3a239e3 100644 --- a/ch02-01-program-unit.html +++ b/ch02-01-program-unit.html @@ -84,7 +84,7 @@ diff --git a/ch03-00-declaration.html b/ch03-00-declaration.html index 2497d1e..ab4644d 100644 --- a/ch03-00-declaration.html +++ b/ch03-00-declaration.html @@ -84,7 +84,7 @@ diff --git a/ch03-01-precision.html b/ch03-01-precision.html index 9bc966b..f44c755 100644 --- a/ch03-01-precision.html +++ b/ch03-01-precision.html @@ -84,7 +84,7 @@ diff --git a/ch03-02-operation.html b/ch03-02-operation.html index 327baf4..76e22c5 100644 --- a/ch03-02-operation.html +++ b/ch03-02-operation.html @@ -84,7 +84,7 @@ diff --git a/ch03-03-array.html b/ch03-03-array.html index 6e1b538..1d620bc 100644 --- a/ch03-03-array.html +++ b/ch03-03-array.html @@ -84,7 +84,7 @@ diff --git a/ch03-04-character-array.html b/ch03-04-character-array.html index ac0fc4b..985db4b 100644 --- a/ch03-04-character-array.html +++ b/ch03-04-character-array.html @@ -84,7 +84,7 @@ diff --git a/ch04-00-if.html b/ch04-00-if.html index d05d9b0..c55205f 100644 --- a/ch04-00-if.html +++ b/ch04-00-if.html @@ -84,7 +84,7 @@ diff --git a/ch04-01-select.html b/ch04-01-select.html index 1d5f3d2..543a708 100644 --- a/ch04-01-select.html +++ b/ch04-01-select.html @@ -84,7 +84,7 @@ diff --git a/ch04-02-do.html b/ch04-02-do.html index 33ba98a..98c7748 100644 --- a/ch04-02-do.html +++ b/ch04-02-do.html @@ -84,7 +84,7 @@ diff --git a/ch04-03-do-array.html b/ch04-03-do-array.html index 94fcc36..1eb908e 100644 --- a/ch04-03-do-array.html +++ b/ch04-03-do-array.html @@ -84,7 +84,7 @@ diff --git a/ch05-00-function.html b/ch05-00-function.html index fa99c0b..bbcce22 100644 --- a/ch05-00-function.html +++ b/ch05-00-function.html @@ -84,7 +84,7 @@ diff --git a/ch05-01-subroutine.html b/ch05-01-subroutine.html index 7dcb47e..1210a42 100644 --- a/ch05-01-subroutine.html +++ b/ch05-01-subroutine.html @@ -84,7 +84,7 @@ diff --git a/ch05-02-funckeyword.html b/ch05-02-funckeyword.html index 64574db..c000d70 100644 --- a/ch05-02-funckeyword.html +++ b/ch05-02-funckeyword.html @@ -84,7 +84,7 @@ diff --git a/ch05-03-callback.html b/ch05-03-callback.html index d9ad9ea..d28b526 100644 --- a/ch05-03-callback.html +++ b/ch05-03-callback.html @@ -84,7 +84,7 @@ diff --git a/ch05-04-module.html b/ch05-04-module.html index 4f2a85c..8ef8870 100644 --- a/ch05-04-module.html +++ b/ch05-04-module.html @@ -84,7 +84,7 @@ diff --git a/ch05-05-overload.html b/ch05-05-overload.html index c4bde31..7d73f70 100644 --- a/ch05-05-overload.html +++ b/ch05-05-overload.html @@ -84,7 +84,7 @@ diff --git a/ch06-00-file.html b/ch06-00-file.html index 8055e83..ef9c4be 100644 --- a/ch06-00-file.html +++ b/ch06-00-file.html @@ -84,7 +84,7 @@ diff --git a/ch06-01-format.html b/ch06-01-format.html index 20c218f..8dcb5e3 100644 --- a/ch06-01-format.html +++ b/ch06-01-format.html @@ -84,7 +84,7 @@ diff --git a/ch07-00-type.html b/ch07-00-type.html index 0c84366..3208bdf 100644 --- a/ch07-00-type.html +++ b/ch07-00-type.html @@ -84,7 +84,7 @@ diff --git a/ch07-01-procbind.html b/ch07-01-procbind.html index e8c12ae..75bd367 100644 --- a/ch07-01-procbind.html +++ b/ch07-01-procbind.html @@ -84,7 +84,7 @@ diff --git a/ch07-02-typeoverload.html b/ch07-02-typeoverload.html index 61b65e5..9a15289 100644 --- a/ch07-02-typeoverload.html +++ b/ch07-02-typeoverload.html @@ -84,7 +84,7 @@ diff --git a/ch08-00-pointer.html b/ch08-00-pointer.html index d73ac51..3de22d6 100644 --- a/ch08-00-pointer.html +++ b/ch08-00-pointer.html @@ -84,7 +84,7 @@ diff --git a/ch08-01-memory.html b/ch08-01-memory.html index 4b71d6f..5c08766 100644 --- a/ch08-01-memory.html +++ b/ch08-01-memory.html @@ -84,7 +84,7 @@ diff --git a/ch08-02-procpointer.html b/ch08-02-procpointer.html index 44c174d..13891ec 100644 --- a/ch08-02-procpointer.html +++ b/ch08-02-procpointer.html @@ -84,7 +84,7 @@ @@ -243,7 +243,7 @@

习题

- @@ -257,7 +257,7 @@

习题

- diff --git a/ch08-03-pointertips.html b/ch08-03-pointertips.html new file mode 100644 index 0000000..26b8483 --- /dev/null +++ b/ch08-03-pointertips.html @@ -0,0 +1,244 @@ + + + + + + 指针的小技巧 - 现代Fortran程序设计 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + +
+
+

指针的小技巧

+

数组的维数变换

+

Fortran中的数组在定义了维数之后,一般是不可变的。这是我们可以使用指针来为其设置一个别名

+
integer,target::a(4,4)
+integer,pointer::pa(:)
+pa(1:size(a))=>a !此时需要设置指针的宽度,且右边只能是一维或者连续的数组
+
+

这时候我们就获得了一个a的一维数组别名

+
integer::i
+pa=[(i,i=1,16)]
+write(*,*)pa(1::size(a,dim=1)) !获取对角项
+
+

如果在子程序或者函数中,我们则需要为数组设置contiguous关键字

+
program main
+    real,target::a(10,10)
+    integer::i
+    real,pointer::p(:)
+    a=reshape([(i,i=1,100)],shape(a)) !使用reshape为数组赋初值
+    p=>diag(a) !使用指针获取对角项
+    write(*,*)p
+    p=[real::(i,i=111,120)] !修改对角项,此时p拿到的是对角项的引用,所以修改了之后,a的对角项也会改变
+    do i=1,10
+        write(*,*)a(i,i)
+    end do
+contains
+    function diag(a)result(ptr)
+        real,intent(in),target,contiguous::a(:,:) !需要加contiguous属性
+        real,pointer::ptr(:)
+        ptr(1:size(a))=>a
+        ptr=>ptr(1::size(a,dim=1)+1) !取对角项
+    end function diag
+end program main
+
+
    +
  • contiguous属性对于数组的连续性做了强制要求,有时候可以使得编译器做出更好的优化,提高代码运行速度,因此在普通的含数组的过程中,也可以选择使用
  • +
+

思考题

+
    +
  • 如果传入的是数组的切片,那么还能用上述的方法修改吗
  • +
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + diff --git a/index.html b/index.html index 9846cab..8a2a0f0 100644 --- a/index.html +++ b/index.html @@ -84,7 +84,7 @@ diff --git a/mdbook-cheat-sheet.html b/mdbook-cheat-sheet.html index 59152f1..f15acfd 100644 --- a/mdbook-cheat-sheet.html +++ b/mdbook-cheat-sheet.html @@ -84,7 +84,7 @@ @@ -181,7 +181,7 @@

数学公式 - @@ -192,7 +192,7 @@

数学公式