苦C復習

//
//  main.c
//  Second
//
//

//*.hファイルとか
#ifndef _INCLUDE_MAIN_
#define _INCLUDE_MAIN_

int sum(int min,int max);
extern int Public;

#endif


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/**
 * ネズミ算
 * 100万まで
 */
int nezumi();

/**
 * staticのテスト
 */
int static_int();

// ポインタ型引数
void increment_int(int *pvalue);

// 同じ
int getaverage1(int data[]);
int getaverage2(int *data);

// struct
typedef struct {
  int year;       /* 学年 */
  int clas;       /* クラス */
  int number;     /* 出席番号 */
  char name[64];  /* 名前 */
  double stature; /* 身長 */
  double weight;  /* 体重 */
} student;

// 配列引数の場合は仮引数名を入れたらだめっぽい
//void test(int[10] hoge);
//void test(int[] hoge);
void showStudent(student *student);
void showStudents(student[], int count);

/**
 * main
 */
int main(int argc, const char *argv[]) {
  printf("Hello, World!\nhoge\n");

  /**
   *forとbreakとif
   */
  for (int i = 0, l = 100; i < l; i++) {
    printf("print %d", i);
    if (i >= 10) {
      printf("break : %d", i);
      break;
    }
  }

  printf("もらったお小遣いの合計が100万円を超えるのは%d月目です\n", nezumi());
  static_int();
  static_int();
  static_int();
  static_int();
  printf("%d\n", static_int());

  // 配列の取り回し
  int array[] = {1, 3, 3, 3, 3, 3, 4};
  for (int i = 0, l = sizeof(array) / sizeof(array[0]); i < l; i++) {
    printf("%d\t", array[i]);
  }
  printf("\n");

  // memory.hとmemcpy

  // 文字列
  char str[] = "MARIO";

  // stdlib.h atoi

  // string.h strcpy

  /*
 関数の側で、そのアドレスにデータを記憶することが出来るわけです。
 これが、scanf関数で変数に&をつける理由です。

 しかし、疑問なのは、文字列を入力する時は、配列名の前に&をつけなかったことです。
 これは、前節で説明したように、配列名は配列の最初の要素のアドレスを表しています。
 */

  // ポインタ!!!!!
  {
    int *p, i;
    p = &i;
    if (p == NULL) {
      //初期化されていないということ
    }
    *p = 10;             //通常モードへ切り替え
    increment_int(&i);   //参照値を渡す
    increment_int(&*p);  //通常モードにしてもう一度参照値に
    printf("%d\n", i);
  }
  {
    student s1;
    s1.clas = 1;
  }
  {
    FILE *file;
    file = fopen("test.txt", "a+");
    for (int i = 0; i < 10; i++) {
      fprintf(file, "hoge %d \n", i);
    }
    fclose(file);
  }
  {
    // malloc
    // #include <stdlib.h>
    int *heap;
    heap = (int *)malloc(sizeof(int) * 10);
    if (heap == NULL) {
      exit(1);
    }
    for (int i = 0; i < 10; i++) {
      heap[i] = i;
    }
    printf("%d\n", heap[3]);
    free(heap);
  }
  {
    printf("hoge\n");
    student *students;
    students = (student *)malloc(sizeof(student) * 10);
    if (students == NULL) {
      exit(1);
    }
    students = (student *)realloc(students, sizeof(student) * 100);
    for (int i = 0, l = 100; i < l; i++) {
      //      students[i].clas = i;
    }
    for (int i = 0, l = 100; i < l; i++) {
      //      printf("class %d\n", students[i].clas);
    }
    /* sizeofで幸せに成るような方法は無いとしれ。
    for (int i = 0, l = sizeof(*students) / sizeof(student); i < l; i++) {
      students[i].clas = i * 2;
    }
    printf("size of %d\n", (int)(sizeof(students) / sizeof(student)));
    for (int i = 0, l = sizeof(*students) / sizeof(student); i < l; i++) {
      printf("class %d\n", students[i].clas);
    }
    */
    free(students);
  }
  {
    student stu;
    stu.clas = 1;
    strcpy(stu.name, "mario");
    stu.number = 1;
    stu.stature = 1.3;
    stu.weight = 4.4;
    stu.year = 2004;
    showStudent(&stu);
    student stu2;
    strcpy(stu2.name, "ruiji");
    student students[] = {stu, stu2};
    showStudents(students, sizeof(students) / sizeof(student));
    printf("class %d\n", students[1].clas);
  }

  return 0;
}

void showStudent(student *stu) { printf("%s\n", (*stu).name); }
void showStudents(student students[], int count) {
  for (int i = 0; i < count; i++) {
    printf("%s\n", students[i].name);
    students[i].clas = i * 10;
  }
}
int nezumi() {
  int sum = 0, tuki = 1, okodukai = 1;
  /** while */
  while (sum < 1000000) {
    sum += okodukai;
    okodukai *= 2;
    tuki++;
  }
  return tuki;
}

int static_int() {
  static int hoge = 0;
  hoge++;
  return hoge;
}

void increment_int(int *pvalue) {
  (*pvalue)++;
  return;
}