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?

データ構造入門(1) - ポインタ・配列・構造体

Last updated at Posted at 2025-10-31

はじめに

こんにちは、Juna1013です。
今日はハロウィンですね(来年は仮装したいです)。

さて、今回は授業で勉強したデータ構造(主にポインタ・配列・構造体)について備忘録的にまとめていきたいと思います。

ポインタとは?

ポインタとは、データそのものではなく、データの場所(アドレス)を指し示すものです。

特徴として、以下が挙げられます。

  1. アドレスを出力できる
  2. アドレス先の値を参照、書き換えできる(関数の参照渡し)

以下に示すのが、ポインタを利用して値を書き換えるプログラムです。

#include <stdio.h>

int main(void) {
    int x = 10;
    int *p1 = &x; // アドレスの値を代入
    printf("%d\n", x); // 10
  
    *p1 = 40;
    printf("%d\n", x); // 40

    return 0;

配列とは?

配列とは、まとまったデータを扱うためのデータ構造です。同じ型のデータ要素から構成されます。

特徴として、以下が挙げられます。

  1. 並び順管理が必要なデータ処理
    • 出席番号の管理、座席順、パーティの並び順
    • トランプや将棋などのゲームデータ
  2. エクセルのような複数行のデータ読み込みなど
    • CSVデータの処理

以下に示すのが、変数と配列を比較したプログラムです。

#include <stdio.h>

int main(void) {
    int x = 10;
    int y = 20;
    int z = 30;
    printf("%d %d %d", x, y, z);

    int data[3] = {10, 20, 30};
    for (int i = 0; i < 2; i++) {
        printf("%d ", data[i]);
    }

    return 0;

構造体(レコード型)とは?

構造体とは、変数をひとまとまりにしたものです。データ構造が異なるものをまとめることができます。
構造体は設計図のようなもので、プログラムで利用する際は実体を作成する必要があります。

特徴として、以下が挙げられます。

  1. 関連のあるデータをまとめることで、ソースコードの可読性が上がる
  2. 宣言した構造体は再利用可能

以下は構造体の使用例です。

#include <stdio.h>

struct student {
        char name[20];
        int age;
        int gender;
};

int main(void) {
    struct student a = {"SUZUKI TARO", 18, "man"};
    struct student b = {"YAMADA HANA", 19, "woman"};

    printf("名前%s, 年齢:%d, 性別:%s\n", a.name, a.age, a.gender);
    printf("名前%s, 年齢:%d, 性別:%s\n", b.name, b.age, b.gender);

    return 0;
}

おわりに

今回は基本的なデータ構造(ポインタ・配列・構造体)についてまとめました。
次回はスタックとキューについてまとめていきたいと思います。

0
0
5

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?