• 検索結果がありません。

郢晄亢縺ケ晢スウ郢ァソ邵コョ郢ァ「郢晏ウィホ樒ケァケ髫ェ閧イョ?br/>

N/A
N/A
Protected

Academic year: 2021

シェア "郢晄亢縺ケ晢スウ郢ァソ邵コョ郢ァ「郢晏ウィホ樒ケァケ髫ェ閧イョ?br/>"

Copied!
3
0
0

読み込み中.... (全文を見る)

全文

(1)

1

ポインタのアドレス計算

学習のポイント  ポインタを1つ進めたとき実際にはアドレス値がどのようになるかを学びます。  さて、今まではpa++やpb++により次のデータを参照できることを暗黙のうちに認 めていました。しかし、int型データが2バイトであることを考えると、pa++によりpa の内容(アドレス)が+1されただけでは次のデータが参照できないことになります。実 はCにおけるポインタ変数の演算(pa++やpa=pa+1)には、ポインタの型により定め られたファクタαが加えられます。 pa = pa + α

 αは一般に、char型なら1、int型なら2、long、float型なら4、double型なら8とな ります。

 たとえばpaがint型のポインタとして、paが0x8000のときpa++にすると、paの 値は0x8002となります。

「C言語」(河西朝雄著 ナツメ社)95頁

(2)

1.1

例題

20

int型データを参照するポインタとchar型データを参照するポインタの演算結果の違い を調べる。 /* 例題20 C言語96頁 int型データを参照するポインタとchar型データを参照するポインタの演算結 果の違いを調べる。 reidai20.c */ #include <stdio.h> int main() {

static int a[] = {0, 1, 2, 3, 4};

static char b[] = {’a’,’b’, ’c’, ’d’, ’e’}; int i; int *pa; char *pb; pa = a; pb = b; for(i = 0; i < 5; i++){

printf("%04x %4d : %04x %c\n", pa, *pa, pb, *pb); pa++; pb++; } return 0; } 2

(3)

1.2

練習問題

20

double型データについて例題20と同じことをするプログラムを作りなさい。このプロ グラムではpa+iという表現でポインタを使うこと。 /* 練習20 C言語97頁 double 型データについて例題 20と同じことをするプログラムを作りなさい。 こ のプログラムではpa+iという表現でポインタを使うこと。 rensyu20.c */ #include <stdio.h> int main() {

static double a[] = {0.0, 1.0, 2.0, 3.0, 4.0}; double *pa;

int i; pa = a;

for(i = 0; i < 5; i++){

printf("%04x %4.1f\n", pa+i, *(pa+i)); }

return 0; }

参照

関連したドキュメント

Here, instead of considering an instance I and trying to directly develop a feasible solution for the P, G ∗ |prec; c ij dπ k , π l 1; p i 1|C max problem, we consider a

There is a bijection between left cosets of S n in the affine group and certain types of partitions (see Bjorner and Brenti (1996) and Eriksson and Eriksson (1998)).. In B-B,

We establish sharp Br´ezis-Gallou¨et-Wainger type inequalities in Besov and Triebel-Lizorkin spaces as well as fractional Sobolev spaces on a bounded domain Ω ⊂ R n.. We treat

(The Elliott-Halberstam conjecture does allow one to take B = 2 in (1.39), and therefore leads to small improve- ments in Huxley’s results, which for r ≥ 2 are weaker than the result

[r]

“Breuil-M´ezard conjecture and modularity lifting for potentially semistable deformations after

lines. Notice that Theorem 4 can be reformulated so as to give the mean harmonic stability of the configuration rather than that of the separate foliations. To this end it is

S., Oxford Advanced Learner's Dictionary of Current English, Oxford University Press, Oxford