構造体とポインタの小さなサンプルメモ

# 構造体サンプルメモ

## シンプル＆メンバを一部省略した場合

```c:test.c
#include <stdio.h>

void main(){

        struct kure_st{
                int num1;
                int num2;
                char m;
        };

        struct kure_st kure = {1, 2, 'h'};

        printf("num1: %d\n", kure.num1);
        printf("num2: %d\n", kure.num2);
        printf("m: %c\n", kure.m);

        //初期値一部省略
        struct kure_st kure2 = {1};
        printf("num1: %d\n", kure2.num1);
        printf("num2: %d\n", kure2.num2);
        printf("m: %c\n", kure2.m);

}
```

```sh:command
./a.out
num1: 1
num2: 2
m: h
num1: 1
num2: 0
m:
```

## 変数名を指定する

```c:test.c
#include <stdio.h>

void main(){

        struct kure_st{
                int num1;
                int num2;
                char m;
        };

        struct kure_st kure = {
                .num2 = 10,
                .m = 'd'
        };

        printf("num1: %d\n", kure.num1);
        printf("num2: %d\n", kure.num2);
        printf("m: %c\n", kure.m);

}
```

```
num1: 0
num2: 10
m: d
```

## typedef

```c:test.c
#include <stdio.h>

void main(){

        typedef struct kure_st{
                int num1;
                int num2;
                char m;
        }kure_st2;

        kure_st2 k = {1, 3, 'h'};
        printf("num1: %d\n", k.num1);
}
```


# ポインタサンプルメモ
## シンプルなサンプル

```c:test.c
#include <stdio.h>

void main(){
        int n;
        int *ptr;
        n = 10;
        ptr = &n;

        printf("n: %d\n", n);
        printf("&n: %p\n", &n);
        printf("ptr: %p\n", ptr);
        printf("*ptr: %d\n", *ptr);
}
```

```sh:command
n: 10
&n: 0x7fff0ded17dc
ptr: 0x7fff0ded17dc
*ptr: 10
```

# 構造体とポインタサンプルメモ

## シンプルなサンプル

```c:test.c
#include <stdio.h>

void main(){

        struct kure_st{
                int num1;
                int num2;
                char m;
        };

        struct kure_st kure = {1, 2, 'h'};

        printf("&kure: %p\n", &kure);
        printf("&num1: %p\n", &kure.num1);
        printf("&num2: %p\n", &kure.num2);
        printf("&m: %p\n", &kure.m);

}
```

```sh:command
&kure: 0x7ffde97282b0
&num1: 0x7ffde97282b0
&num2: 0x7ffde97282b4
&m: 0x7ffde97282b8
```

## メンバへのアクセス方法
### ドット演算子

```c:test.c
#include <stdio.h>

void main(){

        struct kure{
                int n;
                int m;
        };
        struct kure k = {1, 2};
        struct kure *kp = &k;

        printf("(*kp).n: %d\n", (*kp).n);
        printf("(*kp).m: %d\n", (*kp).m);
}
```

```sh:command
 ./a.out
(*kp).n: 1
(*kp).m: 2
```

### アロー演算子

```c:test.c
#include <stdio.h>

void main(){

        struct kure{
                int n;
                int m;
        };
        struct kure k = {1, 2};
        struct kure *kp = &k;

        printf("kp->n: %d\n", kp->n);
        printf("kp->m: %d\n", kp->m);
}
```

```sh:command
kp->n: 1
kp->m: 2
```

### 引数で引き渡す

```c:test.c
#include <stdio.h>

struct kure{
        int n;
        int m;
};

// これだと意味ない
void plusone(struct kure *k){
        int n2 = (*k).n;
        int m2 = (*k).m;
        n2++;
        m2++;
}

void plusone2(struct kure *k){
        (*k).n++;
        (*k).m++;
}

void plusone3(struct kure *k){
        k->n++;
        k->m++;
}


void main(){

        struct kure k = {1, 2};
        struct kure *kp = &k;

        printf("kp->n: %d\n", kp->n);
        printf("kp->m: %d\n", kp->m);

        printf("----plusone----\n");
        plusone(kp);

        printf("kp->n: %d\n", kp->n);
        printf("kp->m: %d\n", kp->m);

        printf("----plusone----\n");
        plusone2(kp);
        printf("kp->n: %d\n", kp->n);
        printf("kp->m: %d\n", kp->m);

        printf("----plusone----\n");
        plusone2(kp);
        printf("kp->n: %d\n", kp->n);
        printf("kp->m: %d\n", kp->m);

}
```

```sh:command
kp->n: 1
kp->m: 2
----plusone----
kp->n: 1
kp->m: 2
----plusone----
kp->n: 2
kp->m: 3
----plusone----
kp->n: 3
kp->m: 4
```