1
0

More than 1 year has passed since last update.

C言語の構造体が分からなかったがなんとなくわかった話

Last updated at Posted at 2021-11-09

結構初歩的な話ですが、自分用メモ。
個人的に、C→Javaは入りやすいイメージですがJava→Cはなんとも。。。
構造体はJavaのクラスのように、構造体の中で定義されたデータ型はメンバ変数のように扱うのです。

※ここではアロー演算子については触れません。

Javaソース

クラスHogeをインスタンス生成することで、Hogeクラスのint型変数の値を出力、書き換えができます。

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Hoge hoge = new Hoge();
        System.out.println(hoge.num);
        hoge.num = 6;
        System.out.println(hoge.num);
    }
}
class Hoge{
    int num = 5;
}

C言語ソース

実体を持つ構造体に限るのでそこだけ注意です。

#include <stdio.h>
int main(void){
    struct student{
        int num;
    };
    struct student student_hoge;

    student_hoge.num = 5;
    printf("%d\n", student_hoge.num);

    return 0;
}

1
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
1
0