0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Accessing array elements 配列の要素へのアクセスの方法

Last updated at Posted at 2020-10-12

int main()
{
    double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
    double salary = balance[4]; //0から4番目 50.0にアクセス
}

double *p1;

p1 = array;
printf("%f", *p1); // 1000.0
printf("%f", *(balance + 2)); //3.4

・arrayのみだと配列の先頭のアドレスを返す

・要素へのアクセス
1、*をつけると先頭の要素を返す
2、[]でインデックスを指定することで、インデックスに相当する要素を返す

//どちらも1000.0を返す
*balance
balance[0]

balance + 2 
/* 
balanceの先頭を指すアドレスが、
2つのインデックス分後ろにずれる。(2番目になる//balanceだと3.4のアドレスを指す)
*/

その要素にアクセスするには

*(balance + 2) // 3.4
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?