Skip to content

Commit

Permalink
誤記修正.
Browse files Browse the repository at this point in the history
  • Loading branch information
htakeuchi0 committed Sep 17, 2022
1 parent 40602f6 commit d372098
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
24 changes: 17 additions & 7 deletions content/docs/cpp/arrp/arrp.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int *p = arr;

## 配列とポインタの関係に関する注意

先述の通り,配列とポインタは密接な関係があるといえる.一方で,`int` 型へのポインタ型変数 `p` に代入した場合,配列 `arr` 全体のサイズを得ることはできません.**なぜなら,`p` はあくまで `int` 型へのポインタ型なのでもとの配列の情報はもたないからです**
先述の通り,配列とポインタは密接な関係があるといえます.一方で,`int` 型へのポインタ型変数 `p` に代入した場合,配列 `arr` 全体のサイズを得ることはできません.**なぜなら,`p` はあくまで `int` 型へのポインタ型なのでもとの配列の情報はもたないからです**

具体的には,

Expand All @@ -112,7 +112,9 @@ void func(int a[100]) {
}
```
よって,一般に,配列型変数を引数にする場合,引数の要素数は使われないので省略可能です.
### 配列を引数にもつ関数
一般に,配列型変数を引数にする場合,引数の要素数は使われないので省略可能です.
```cpp
void func(int a[]) {
Expand All @@ -128,7 +130,7 @@ void func(int *a) {
}
```
同様に,**引数で要素数を明示しても,要素数100以外の配列を渡すときにエラーは発生しません**.
同様の理由で,**引数で要素数を明示しても,要素数100以外の配列を渡すときにエラーは発生しません**.
```cpp
#include <iostream>
Expand All @@ -146,9 +148,11 @@ int main() {

## 配列型へのポインタ型

配列を指すポインタとして,`int` 型の配列型に対して,`int` 型へのポインタ型があることを説明しましたが,これは先頭要素を指しているだけのため,`*` 演算子で得られるのは先頭要素だけで,特に,要素数の情報は失われます.
`int` 型配列を指すポインタとして,**`int` 型への**ポインタについて説明しましたが,これは先頭要素を指しているだけのため,`*` 演算子で得られるのは先頭要素だけで,特に,要素数の情報は失われるのでした.また,配列を引数にとる関数をつくろうとすると,先頭要素のポインタしか渡されないとわかりました.

そこで,`int` 型へのポインタではなく,**配列型への**ポインタ型というものがあれば,`*` 演算子で値を得ると,配列そのものが得られるので,要素数の情報も失われないと考えられます.

そこで,先頭要素ではなく,配列型のポインタ型というものを考えます.これがあれば,`*` 演算子で値を得ると,配列そのものが得られるので,要素数の情報も失われないと考えられます.
### 配列型へのポインタ型の宣言

クラス `T` の要素数 `N` の配列型へのポインタ変数 `p_arr` の宣言は,

Expand Down Expand Up @@ -198,6 +202,8 @@ arr_t *p_arr;

となります.

### 配列ポインタへの代入と要素の参照

`int arr[100]` の配列ポインタへの代入と,要素の参照は,

```cpp
Expand Down Expand Up @@ -260,7 +266,7 @@ std::size_t ArraySize(int (*pa)[100]) {
ただし,この方法では,配列の型と要素数ごとに `ArraySize` 関数を定義する必要があります.
C++のテンプレートでは,テンプレート引数として,整数値をとることができます.これを利用すると,`ArraySize` は関数テンプレートを使って以下のように書き直せます.
ところで,C++のテンプレートでは,テンプレート引数として,整数値をとることができます.これを利用すると,`ArraySize` は関数テンプレートを使って以下のように書き直せます.
```cpp
#include <cstddef>
Expand Down Expand Up @@ -304,6 +310,10 @@ int main() {
}
```

よって,関数形式マクロでなく,配列の要素数を返す関数(テンプレート)が作れました.

## まとめ

本ページでは,C++における配列とポインタの関係を説明した後,配列型へのポインタ型について説明しました.C++では配列とポインタの間には密接な関係がありますが,型としてはまったく同じものではありません.配列型へのポインタ型を定義することができ,これを利用すると,関数形式マクロでなく,配列の要素を返す関数テンプレートが定義できます.
本ページでは,C++における配列とポインタの関係を説明した後,配列型へのポインタ型について説明しました.C++では配列とポインタの間には密接な関係がありますが,型としては同じものではありません.

一方で,配列型へのポインタ型を定義することができ,これを利用すると,関数形式マクロでなく,配列の要素を返す関数テンプレートが定義できることがわかりました.
4 changes: 2 additions & 2 deletions content/docs/cpp/fp/fp.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ double Newton(double x0, double eps=1.0e-10, std::size_t loop_max=1024) {
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

int main() {
Expand Down Expand Up @@ -218,7 +218,7 @@ double Newton(fp_t fp, fp_t dfp, double x0, double eps=1.0e-10,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

double f(double x) {
Expand Down
2 changes: 1 addition & 1 deletion content/docs/cpp/fp/fref.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ double Newton(fref_t f, fref_t df, double x0, double eps=1.0e-10,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

double f(double x) {
Expand Down
8 changes: 4 additions & 4 deletions content/docs/cpp/fp/mfp.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ double Newton(const Polynomial& f, const Polynomial& df, double x0, double eps=1
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

int main() {
Expand Down Expand Up @@ -132,7 +132,7 @@ double Newton(const Evaluate& f, const Evaluate& df, double x0, double eps=1.0e-
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}
```

Expand Down Expand Up @@ -193,7 +193,7 @@ double Newton(const Polynomial& f, const Polynomial& df, mfp_t mfp,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}
int main() {
Expand Down Expand Up @@ -262,7 +262,7 @@ double Newton(const T& f, const T& df, tmfp_t<T> tmfp,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

int main() {
Expand Down
12 changes: 6 additions & 6 deletions content/docs/cpp/fp/stdfunc.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ double Newton(func_t f, func_t df, double x0, double eps=1.0e-10,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}
```

Expand All @@ -72,7 +72,7 @@ double Newton(func_t f, func_t df, double x0, double eps=1.0e-10,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

double f(double x) {
Expand Down Expand Up @@ -181,7 +181,7 @@ double Newton(const T& f, const T& df, mfunc_t<T> eval, double x0, double eps=1.
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}
```
Expand Down Expand Up @@ -232,7 +232,7 @@ double Newton(const T& f, const T& df, mfunc_t<T> eval, double x0, double eps=1.
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}
int main() {
Expand Down Expand Up @@ -284,7 +284,7 @@ double Newton(func_t f, func_t df, double x0, double eps=1.0e-10,
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}


Expand Down Expand Up @@ -326,7 +326,7 @@ double Newton(const T& f, const T& df, mfunc_t<T> eval, double x0, double eps=1.
return x;
}
}
throw std::runtime_error("Not convergence.");
throw std::runtime_error("Convergence error: This method did not converge.");
}

double f(double x) {
Expand Down

0 comments on commit d372098

Please sign in to comment.